npm install once-emitted
yarn add once-emitted
Creates a promise that listens to emitter
for an event with the name eventName
.
The promise will reject if the timeout
(milliseconds) is reached without the event firing or without resolveOn
or rejectOn
returning true
.
The once
function will pass the emitter listener arguments through to resolveOn
and rejectOn
.
import { once } from 'once-emitted';
import { EventEmitter } from 'events';
const emitter = new EventEmitter();
const promise = once(emitter, 'statusChange', {
timeout: 10000,
resolveOn: (status) => (status === 'initialized'),
rejectOn: (status) => (['timedOut', 'unauthorized'].includes(status))
});
emitter.emit('statusChange', 'initialized');
const result = await promise;
// { result: ['initialized'] }