如何从异步函数返回吸引力
我需要返回吸引力
( https:///www.npmjs .com/package/web3核 - 促进)来自async
函数。但是我总是在处理程序上没有没有
的普通。这是测试代码: const web3promivent = require('web3-core-promievent')
async function sleep() {
return new Promise((resolve) => setTimeout(resolve, 1000))
}
async function inner() {
const pe = new Web3PromiEvent()
await sleep()
pe.eventEmitter.emit('done', 'ok')
pe.resolve('ok')
return pe
}
async function main() {
const pe = inner()
pe.on('done', console.log)
const res = await pe
console.log(res)
}
main()
这里的示例: https://codesandbox.io/s/node-js-forked-lw19bp?file=/src/index.js
async
功能?
I need to return PromiEvent
(https://www.npmjs.com/package/web3-core-promievent) from async
function. But I always got a common Promise
without on
handlers. Here is the test code:
const Web3PromiEvent = require('web3-core-promievent')
async function sleep() {
return new Promise((resolve) => setTimeout(resolve, 1000))
}
async function inner() {
const pe = new Web3PromiEvent()
await sleep()
pe.eventEmitter.emit('done', 'ok')
pe.resolve('ok')
return pe
}
async function main() {
const pe = inner()
pe.on('done', console.log)
const res = await pe
console.log(res)
}
main()
Here the example in sandbox: https://codesandbox.io/s/node-js-forked-lw19bp?file=/src/index.js
How properly return PromiEvent
from async
function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论