Polyfill for Node core events.once
.
events.once
can be used directly or can polyfill
the events module.
require('events.once/polyfill')
const { once, EventEmitter } = require('events')
async function run() {
const ee = new EventEmitter()
process.nextTick(() => {
ee.emit('myevent', 42)
})
const [value] = await once(ee, 'myevent')
console.log(value)
const err = new Error('kaboom')
process.nextTick(() => {
ee.emit('error', err)
})
try {
await once(ee, 'myevent')
} catch (err) {
console.log('error happened', err)
}
}
run()
Instead of requiring directly it can also be preloaded when starting a process:
node -r events.once/polyfill my-app.js
const once = require('events.once')
const { EventEmitter } = require('events')
async function run() {
const ee = new EventEmitter()
process.nextTick(() => {
ee.emit('myevent', 42)
})
const [value] = await once(ee, 'myevent')
console.log(value)
const err = new Error('kaboom')
process.nextTick(() => {
ee.emit('error', err)
})
try {
await once(ee, 'myevent')
} catch (err) {
console.log('error happened', err)
}
}
run()
Yes
Yes
MIT