events.once
DefinitelyTyped icon, indicating that this package has TypeScript declarations provided by the separate @types/events.once package

2.0.2 • Public • Published

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

Package Sidebar

Install

npm i events.once

Weekly Downloads

1,561

Version

2.0.2

License

MIT

Unpacked Size

5.44 kB

Total Files

6

Last publish

Collaborators

  • davidmarkclements