https://github.com/valesdev/events-promisify
Just another JavaScript EventEmitter for Promise.
https://github.com/valesdev/events-promisify
eventemitter events promise
Last synced: 8 months ago
JSON representation
Just another JavaScript EventEmitter for Promise.
- Host: GitHub
- URL: https://github.com/valesdev/events-promisify
- Owner: valesdev
- License: mit
- Created: 2019-07-27T02:26:56.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2022-03-07T12:47:44.000Z (over 4 years ago)
- Last Synced: 2024-08-09T23:47:35.049Z (almost 2 years ago)
- Topics: eventemitter, events, promise
- Language: TypeScript
- Homepage:
- Size: 8.79 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# events-promisify
[](https://www.npmjs.com/package/events-promisify)
[](https://npmcharts.com/compare/events-promisify?minimal=true)
[](https://www.npmjs.com/package/events-promisify)
Just another JavaScript EventEmitter for Promise.
## Installation
### Node.js
```sh
$ npm install --save events-promisify
```
### Browser via CDN
```html
```
## Usage
```js
import EventEmitter from 'events-promisify'
const ee = new EventEmitter()
ee.on('event-a', function () {
/**
* listener can return a Promise instance
* all listeners will be executed serially
*/
return new Promise(function (resolve, reject) {
/** resolve asynchronously */
setTimeout(function () {
resolve('the result')
}, 1e3)
/**
* ...or reject with an Error instance
* will cause the emitter rejected immediately
*/
// reject(new Error('Failed!'))
})
})
ee.once(['event-a', 'event-b'], function (arg1, arg2) {
/** ...or return anything */
return `foo ${arg1} bar ${arg2}`
})
ee.on('event-b', function () {
/**
* ...or throw an Error instance
* also will cause the emitter rejected immediately
*/
throw new Error('Failed!')
})
ee.emit('event-a', 11, 22)
.then(function (results) {
/** event finished */
})
.catch(function (error) {
/** event failed with error */
})
```
## API
### `ee.on(, )`
Listen on events.
| Parameter | Type | Description | Default |
| --- | --- | --- | --- |
| `eventName` | String\|String[] | The event name or array of event names. | (required) |
| `func` | Function | The listener function. | (required) |
- Returns: `void`
### `ee.once(, )`
Listen on events for one-time execution.
| Parameter | Type | Description | Default |
| --- | --- | --- | --- |
| `eventName` | String\|String[] | The event name or array of event names. | (required) |
| `func` | Function | The listener function. | (required) |
- Returns: `void`
### `ee.off(, )`
Remove listener for events.
| Parameter | Type | Description | Default |
| --- | --- | --- | --- |
| `eventName` | String\|String[] | The event name or array of event names. | (required) |
| `func` | Function | The listener function. | (required) |
- Returns: `void`
### `ee.emit(, [...args])`
Listen on events.
| Parameter | Type | Description | Default |
| --- | --- | --- | --- |
| `eventName` | String | The event name. | (required) |
| `args` | ...any | Arguments passed to listeners. | |
- Returns: `Promise`
## License
[MIT](http://opensource.org/licenses/MIT)