events-promisify
v3.0.0
Published
Just another JavaScript EventEmitter for Promise.
Downloads
5
Readme
events-promisify
Just another JavaScript EventEmitter for Promise.
Installation
Node.js
$ npm install --save events-promisify
Browser via CDN
<script src="https://cdn.jsdelivr.net/npm/events-promisify/dist/index.umd.js"></script>
Usage
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(<eventName>, <func>)
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(<eventName>, <func>)
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(<eventName>, <func>)
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(<eventName>, [...args])
Listen on events.
| Parameter | Type | Description | Default |
| --- | --- | --- | --- |
| eventName
| String | The event name. | (required) |
| args
| ...any | Arguments passed to listeners. | |
- Returns:
Promise<any>