1.0.0 • Published 4 years ago

strict-event-emitter-once v1.0.0

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

strict-event-emitter-once

NPM Package Build Status

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 === '/';
});

LICENSE MIT