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?