strict-event-emitter-once
v1.0.0
Published
Promise based `once` for strict-event-emitter
Downloads
2
Maintainers
Readme
strict-event-emitter-once
A typed implementation of Node.js events.once(emitter, name)
for use with strict-event-emitter-types
.
Creates a Promise that is fulfilled when the EventEmitter emits the given event.
Example usage
import { once } from 'strict-event-emitter-once';
// setup typed event emitter
import { StrictEventEmitter } from 'strict-event-emitter-types';
import { EventEmitter } from 'events';
// define your events
interface Events {
request: (request: Request, response: Response) => void;
done: void;
}
// create strict event emitter types
const ee: StrictEventEmitter<EventEmitter, Events> = new EventEmitter;
// await for an event
const [request, response] = await once(ee, 'request');
// await for an event that matches criteria
const [request, response] = await once(ee, 'request', (req, res) => {
return req.url === '/';
});