npm

npm’s 2019 JavaScript ecosystem survey analysis is now available!Get your copy here »

events.once

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

install

npm i events.once

Downloadsweekly downloads

160

version

2.0.2

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability