events.once
v2.0.2
Published
Polyfill for Node core `events.once
Downloads
7,833
Maintainers
Readme
events.once
Polyfill for Node core events.once
.
Usage
events.once
can be used directly or can polyfill
the events module.
Polyfill
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
As an export
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()
Does it work with WebPack?
Yes
Does it work with Browserify?
Yes
License
MIT