https://github.com/primus/asyncemit
Asynchronously emit event an event based on the arguments length.
https://github.com/primus/asyncemit
Last synced: 12 months ago
JSON representation
Asynchronously emit event an event based on the arguments length.
- Host: GitHub
- URL: https://github.com/primus/asyncemit
- Owner: primus
- License: mit
- Created: 2015-03-30T07:56:22.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2023-01-02T20:01:11.000Z (over 3 years ago)
- Last Synced: 2025-06-01T13:15:59.688Z (about 1 year ago)
- Language: JavaScript
- Size: 29.3 KB
- Stars: 4
- Watchers: 5
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# asyncemit
[](https://www.npmjs.com/package/asyncemit)[](https://github.com/primus/asyncemit/actions?query=workflow%3ACI+branch%3Amaster)[](https://coveralls.io/r/primus/asyncemit?branch=master)
The `asyncemit` allows you to emit an event to an EventEmitter3 asynchronously.
## Installation
The module is released in the public npm registry and can be installed using:
```
npm install --save asyncemit
```
## Usage
To make this pattern work there are a couple of preconditions that need to be
satisfied:
1. The method should be added on either a class that inherits from the
EventEmitter or on a new EventEmitter instance.
2. The number of arguments expected by a listener function should match the
number of arguments passed to the `asyncemit` method excluding the event
name.
See the following example:
```js
var EventEmitter = require('eventemitter3')
, asyncemit = require('asyncemit');
var ee = new EventEmitter();
ee.asyncemit = asyncemit;
//
// The next `foo` listeners will not be executed until `next` is called.
//
ee.on('foo', function (arg, next) {
// Do things with arg?
next();
});
//
// Still executed, but synchronously.
//
ee.on('foo', function (arg) {
});
ee.asyncemit('foo', 'bar', function (err) {
//
// The error argument will be set if one of the async listeners called the
// `next` callback with an `error` argument.
//
});
```
## License
[MIT](LICENSE)