# await-event-emitter

> Await events library like EventEmitter

Latest version **2.0.2** (published 2020-11-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install await-event-emitter
pnpm add await-event-emitter
yarn add await-event-emitter
bun add await-event-emitter
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.2 |
| Published | 2020-11-24 |
| First published | 2018-01-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=10 |
| Dependencies | 1 |
| Unpacked size | 20.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 21 |
| Author | imcuttle |
| Maintainers | moyuyc |
| Keywords | imcuttle, node, await, event, emitter, node-await-event-emitter |

## Links

- npm: https://www.npmjs.com/package/await-event-emitter
- Repository: https://github.com/imcuttle/node-await-event-emitter
- Homepage: https://github.com/imcuttle/node-await-event-emitter#readme
- Issues: https://github.com/imcuttle/node-await-event-emitter/issues
- npm.io page: https://npm.io/package/await-event-emitter

## Dependencies (1)

- [is-promise](https://npm.io/package/is-promise.md) ^4.0.0

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 2.0.2 (latest) — 2020-11-24
- 2.0.1 — 2020-11-03
- 1.2.0 — 2018-11-08
- 1.1.3 — 2018-05-06
- 1.1.2 — 2018-04-05
- 1.1.1 — 2018-03-28
- 1.1.0 — 2018-01-21
- 1.0.4 — 2018-01-21
- 1.0.3 — 2018-01-21
- 1.0.2 — 2018-01-21
- 1.0.1 — 2018-01-21

## README

# await-event-emitter

Await events library like EventEmitter

[![build status](https://img.shields.io/travis/imcuttle/node-await-event-emitter/master.svg?style=flat-square)](https://travis-ci.org/imcuttle/node-await-event-emitter)
[![Test coverage](https://img.shields.io/codecov/c/github/imcuttle/node-await-event-emitter.svg?style=flat-square)](https://codecov.io/github/imcuttle/node-await-event-emitter?branch=master)
[![NPM version](https://img.shields.io/npm/v/await-event-emitter.svg?style=flat-square)](https://www.npmjs.com/package/await-event-emitter)
[![NPM Downloads](https://img.shields.io/npm/dm/await-event-emitter.svg?style=flat-square&maxAge=43200)](https://www.npmjs.com/package/await-event-emitter)

## Why?

The concept of Webpack plugin has lots of lifecycle hooks, they implement this via EventEmitter.
In the primitive [events](https://nodejs.org/dist/latest/docs/api/events.html) module on nodejs, the usage as follows

```javascript
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

```bash
npm install --save await-event-emitter
```

## Usage

```javascript
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

```bash
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, <a href="mailto:imcuttle@163.com">imcuttle@163.com</a>.

## License

MIT - [imcuttle](https://github.com/imcuttle) 🐟

---
_Source: https://npm.io/package/await-event-emitter · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
