Skip to content

The async/await event emitters, which is a drop-in replacement of the nodejs' builtin events.

License

Notifications You must be signed in to change notification settings

kaelzhang/node-async-ee

Repository files navigation

Build Status Coverage

async-ee

The async/await event emitters, which is a drop-in replacement of the nodejs' builtin events.

Install

$ npm install async-ee

Usage

The basic usage of async-ee is totally the same as the vanilla EventEmitter. You could simply use async-ee instead events in the existing code.

async-ee extends EventEmitter, and provides two new methods:

  • emitAsync
  • emitAsyncSeries
import EventEmitter from 'async-ee'

class MyClass extends EventEmitter {
  async doSomething () {
    await this.emitAsync('before-event')
    const data = await doThings()
    await this.emitAsync('after-event', data)
    this.emit('sync-event')
    return data
  }
}

const foo = new MyClass()
.on('before-some-event', async () => await validateFromRemoveServer())
.on('after-event', async data => await doTheTracking(data))
.on('sync-event', () => console.log('did something'))

;(async function () {
  const data = await foo.doSomething()
})

emitter.emitAsync(eventName[, ...args])

Run event handlers with Promise.all().

emitter.emitAsyncSeries(eventName[, ...args])

Run async event handlers in sequences.

License

MIT

About

The async/await event emitters, which is a drop-in replacement of the nodejs' builtin events.

Resources

License

Stars

Watchers

Forks

Packages

No packages published