await-event-or-error

1.0.1 • Public • Published

Actions Status

await-event-or-error

Returns a Promise that resolves on an event or rejects on an error.

Examples

Pass an EventEmitter and an event to be watched.

await awaitEventOrError(emitter, "connected");

The function can be added to an EventEmitter to allow only passing the event.

emitter.eventOrError = awaitEventOrError;
await emitter.eventOrError("connected");

The promise will reject on an "error" event by default, but you can specify a different error event.

await emitter.eventOrError("connected", "responseError");

If the event emits values they will be returned in an array.

const values = await emitter.eventOrError("connected");
...
emitter.emit("connected", 1, 2);
...
// values === [1, 2]

Package Sidebar

Install

npm i await-event-or-error

Weekly Downloads

1

Version

1.0.1

License

MIT

Unpacked Size

16.5 kB

Total Files

13

Last publish

Collaborators

  • tonybrix