Emit an event and waiting for a result. Based on eventemitter3.
By package manager:
npm install eventemitter-callback --save
# For Yarn, use the command below.
yarn add eventemitter-callback
Installation from CDN:
<!-- For UNPKG use the code below. -->
<script src="https://unpkg.com/eventemitter-callback"></script>
<!-- For JSDelivr use the code below. -->
<script src="https://cdn.jsdelivr.net/npm/eventemitter-callback"></script>
<script>
// UMD module is exposed through the "EventEmitterCB" global variable.
console.log(EventEmitterCB);
</script>
import emitter from 'eventemitter-callback'
// import * as emitter from 'eventemitter-callback' // type: 'module'
emitter.on('event-name', (arg) => {
console.log(arg) // prints "ping"
return 'pong'
})
// emit an event and waiting for its result
emitter.emit('event-name', 'ping', (arg) => {
console.log(arg) // prints "pong"
})
By default, the following works normally:
emitter.emit('msg', 1)
emitter.on('msg', (arg) => {
console.log(arg) // prints 1
})
If this doesn't meet your expectations, you can disable it:
emitter.disablePreEmitter();
emitter.emit('msg', 1)
emitter.on('msg', (arg) => {
console.log(arg) // will not be called!
})
- on(eventName, listener)
- once(eventName, listener)
- emit(eventName[, ...args][, callback])
- emitAll(eventName[, ...args])
- emitValidateAll(eventName[, ...args])
- emitValidateAnyOf(eventName[, ...args])
- off(eventName[, listener])
- disablePreEmitter()
- enablePreEmitter()
Adds the listener
function to the end of the listeners array for the event named eventName
.
- eventName
string
the name of the event - listener
function
the callback function
The listener
can be asynchronous. Specified as an AsyncFunction
or return a Promise
.
emitter.on('event-name', (arg) => {
console.log(arg) // prints "ping"
return new Promise((resolve) => {
resolve('pong')
})
})
// emitter.on('event-name', async (arg) => {
// console.log(arg) // prints "ping"
// const res = await queryRemote() // suppose res returns 'pong'
// return res
// })
emitter.emit('event-name', 'ping', (arg) => {
console.log(arg) // prints "pong"
})
- eventName
string
the name of the event - listener
function
the callback function
Adds a one-time listener
function for the event named eventName
.
- eventName
string
the name of the event - ...args
any
- callback
any
the callback function
Returns true
if the event had listeners, false
otherwise.
- eventName
string
the name of the event - ...args
any
Trigger all the listeners at once. Returns a result array by Promise.all
.
- eventName
string
the name of the event - ...args
any
Trigger all the listeners at once and check the values returned.
Returns true
if all listeners returns true, false
otherwise.
- eventName
string
the name of the event - ...args
any
Trigger all the listeners at once and check the values returned.
Returns true
if either listener returns true, false
otherwise.
- eventName
string
the name of the event - listener
function
the callback function
Removes the specified listener
from the listener array for the event named eventName
.
Removes all the listeners for the event:
emitter.off('event-name')
Disable triggering events before listening.
Enable triggering events before listening.
Reference to current instance of EventEmitter3.
Reference to constructor of EventEmitter3.
MIT, see the LICENSE file for detail.