Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

await-event-emitter

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

await-event-emitter

Await events library like EventEmitter

  • 2.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
8.7K
decreased by-20.8%
Maintainers
1
Weekly downloads
 
Created
Source

await-event-emitter

Await events library like EventEmitter

build status Test coverage NPM version NPM Downloads

Why?

The concept of Webpack plugin has lots of lifecycle hooks, they implement this via EventEmitter. In the primitive events module on nodejs, the usage as follows

const EventEmitter = require('events')
const emitter = new EventEmitter()

emitter
  .on('event', () => {
    // do something *synchronously*
  })
  .emit('event', '...arguments')

The listener must be synchronous, that is way i wrote it.
And await-event-emitter support synchronous emitter magically :smile:

Installation

npm install --save await-event-emitter

Usage

const AwaitEventEmitter = require('await-event-emitter').default

const emitter = new AwaitEventEmitter()
const tick = () =>
  new Promise((resolve) => {
    setTimeout(() => {
      console.log('tick')
      resolve()
    }, 1000)
  })

emitter.on('event', async () => {
  // wait to print
  await tick()
})

async function run() {
  // NOTE: it's important to `await` the reset process
  await emitter.emit('event', '...arguments')
  await emitter.emit('event', 'again')

  // support emit it synchronously
  emitter.emitSync('event', 'again')
}

run()

API

Class AwaitEventEmitter

  • addListener(event, listener) : AwaitEventEmitter
    alias: on
  • once(event, listener)
  • prependListener(event, listener) : AwaitEventEmitter
    alias: prepend
  • prependOnceListener(event, listener) : AwaitEventEmitter
    alias: prependOnce
  • removeListener(event, listener) : AwaitEventEmitter
    alias: off
  • listeners(event) : []
  • emit(event, ...args) : Promise.resolve(boolean)
    emit listeners asynchronously, we recommended await it resolved the result
  • emitSync(event, ...args) : boolean emit listeners synchronously

Test

npm test

Contributing

  • Fork it!
  • Create your new branch:
    git checkout -b feature-new or git checkout -b fix-which-bug
  • Start your magic work now
  • Make sure npm test passes
  • Commit your changes:
    git commit -am 'feat: some description (close #123)' or git commit -am 'fix: some description (fix #123)'
  • Push to the branch: git push
  • Submit a pull request :)

Authors

This library is written and maintained by imcuttle, imcuttle@163.com.

License

MIT - imcuttle 🐟

Keywords

FAQs

Package last updated on 24 Nov 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc