Skip to content

davidmarkclements/events.once

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Polyfill for Node core events.once

Resources

License

Stars

Watchers

Forks

Packages

No packages published