events.once
Polyfill for Node core `events.once
Last updated 2 years ago by davidmarkclements .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install events.once 
SYNC missed versions from official npm registry.

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

Current Tags

  • 2.0.2                                ...           latest (2 years ago)

6 Versions

  • 2.0.2                                ...           2 years ago
  • 2.0.1                                ...           2 years ago
  • 2.0.0                                ...           2 years ago
  • 1.0.2                                ...           2 years ago
  • 1.0.1                                ...           2 years ago
  • 1.0.0                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 7
This Month 34
Last Day 1
Last Week 15
Last Month 18
Dependencies (0)
None
Dev Dependencies (1)

Copyright 2014 - 2017 © taobao.org |