【问题标题】:What is the commonjs equivalent of doing var = return await myAsyncFunction() outside of asyncfunction?在异步函数之外执行 var = return await myAsyncFunction() 的 commonjs 等价物是什么?
【发布时间】:2022-10-12 22:22:37
【问题描述】:

我正在尝试将我的快速路由器功能连接到 ReactJS。目前我的代码看起来像这样:

nftmodule.js

import {ZDK} from '@zoralabs/zdk'




const zdk = new ZDK("https://api.zora.co/graphql")

export async function fetchTokens(zdk, collectionAddresses){
  return await zdk.tokens({
    where: {
      collectionAddresses
    }
  })
} 

const tokens = await fetchTokens(zdk, '0x42069ABFE407C60cf4ae4112bEDEaD391dBa1cdB')

export function aToken(){
  return tokens
}

然后它弹出到这里

nftholecollection.js

import { aToken, fetchTokens} from './nftmodule.js'
import {ZDK} from '@zoralabs/zdk'

const zdk = new ZDK("https://api.zora.co/graphql")

let token = await fetchTokens(zdk, '0x42069ABFE407C60cf4ae4112bEDEaD391dBa1cdB')
let thistoken = JSON.stringify(token,null,3)
console.log(JSON.parse(thistoken).tokens.nodes[3].token.image)


let collectionSize = 40;


const tokens = aToken()

let nftGallery = JSON.stringify(tokens,null,3)
let x

export function loopLinks()
{
  let output =""
    for(x =0;x<collectionSize; x++)
    if (JSON.parse(nftGallery).tokens.nodes[x].token.image.mediaEncoding.__typename != "UnsupportedEncodingTypes") {
    output = output + `<img src=${JSON.stringify(JSON.parse(nftGallery).tokens.nodes[x].token.image.mediaEncoding.thumbnail)} loading='lazy'>`
    }
    return output
}
console.log(loopLinks())

然后最后nftrouter.js

import {loopLinks} from './nftwholecollection.js'
import express from 'express'

console.log(loopLinks())


const app = express();
const port = 5150;


app.get('/api/nft',(req,res)=>{
  res.setHeader("Content-Type", "text/html")
  res.write(loopLinks())
  res.end()
})

app.listen( port ,()=>{
    console.log("the server got 5150'd")
});

看来我在这里有两个选择。我可以弄清楚如何让我的模块出现在 react.js 中,或者我可以弄清楚如何在 commonjs 中编写上述代码。 我正在尝试做后者。

我遇到了几个问题,基本上都围绕着试图找出常见的 js 等价物“let token = await fetchTokens(zdk, 'blablablacrypto')”

当我尝试在 packagejson 中将类型设置为 commonjs 时,我显然在 nftmodule 中遇到了一个错误,即无法在 commonjs 中的异步函数主体之外引用 await。什么是 commonjs 解决方案?

【问题讨论】:

  • 你好。我不明白您将看似完美的 Express-in-ES-modules 实现更多地用于 CommonJS 的动机。只需在 ES Modules 中执行 Express 即可,服务器将正常工作。然后从 React 中获取。我在这里想念什么?
  • 使用 promise.then 语法 ... 或 (aysnc() =&gt; { your code that uses await })() - 当然,无论哪种方式,您都需要对代码进行其他一些重写
  • 您已经在nftmodulenftwholecollection 之间创建了一个循环依赖关系。我建议先删除它。
  • @morganney 啊,我在 nftrouter 代码中复制了两次!我的错误我将编辑 psot
  • 如果您想依靠顶级等待来确保令牌可用于依赖模块,请在 nftmodule.js 中执行 export default await fetchTokens()。然后其他模块导入导出的token,而不是用于获取令牌的函数。

标签: javascript node.js async-await top-level-await


【解决方案1】:

我会尝试这样的事情来使用顶级等待。

nftmodule.js

import {ZDK} from '@zoralabs/zdk'

const zdk = new ZDK("https://api.zora.co/graphql")

async function fetchTokens(zdk, collectionAddresses){
  return await zdk.tokens({
    where: {
      collectionAddresses
    }
  })
} 

export default await fetchTokens(zdk, '0x42069ABFE407C60cf4ae4112bEDEaD391dBa1cdB')

nftholecollection.js

// This module will wait for the promise to resolve before executing
import tokens from './nftmodule.js'

let collectionSize = 40;

let nftGallery = JSON.stringify(tokens,null,3)
let x

export function loopLinks()
{
  let output =""
    for(x =0;x<collectionSize; x++)
    if (JSON.parse(nftGallery).tokens.nodes[x].token.image.mediaEncoding.__typename != "UnsupportedEncodingTypes") {
    output = output + `<img src=${JSON.stringify(JSON.parse(nftGallery).tokens.nodes[x].token.image.mediaEncoding.thumbnail)} loading='lazy'>`
    }
    return output
}
console.log(loopLinks())

nftrouter.js(和之前一样)

import {loopLinks} from './nftwholecollection.js'
import express from 'express'

console.log(loopLinks())


const app = express();
const port = 5150;


app.get('/api/nft',(req,res)=>{
  res.setHeader("Content-Type", "text/html")
  res.write(loopLinks())
  res.end()
})

app.listen( port ,()=>{
    console.log("the server got 5150'd")
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-04
    • 2018-03-03
    • 2011-07-23
    • 1970-01-01
    • 2017-02-02
    • 2021-03-05
    • 1970-01-01
    • 2022-08-21
    相关资源
    最近更新 更多