promise-once-events
This module provides promisified version of standard
EventEmitter
class,
except that
once
method returns Promise
object which is fulfilled when
emit
method is called.
Requirements
This module requires Node >= 4. For Node < 6 --harmony
flag is required.
Installation
npm install promise-once-events
Usage
promise-once-events
can be a base class for custom event emitter.
const PromiseOnceEvents = {} const emitter =
Method once
returns Promise
object which is fulfilled when emit
method is
called and then result is an Arguments
object which contains arguments from
emit
method.
// As promiseemitter emitter
It also works with async
/await
syntax:
// As promiseconst result = await emitter// result is object Argumentsconsole
If the last argument for once
method is a callback then it works as for
original EventEmitter.once
method.
// With callbackemitter emitter
Overriding EventEmitter
PromiseOnceEvents
can be used as a replacement for EventEmitter
for existing
objects.
Example:
const readable = fs readableonce = PromiseOnceEventsprototypeonce readable await readableconsole
Promise
This module uses any-promise and any ES6 Promise library or polyfill is supported.
Ie. bluebird can be used as Promise library for this module, if it is registered before.
const PromiseOnceEvents =
License
Copyright (c) 2016-2017 Piotr Roszatycki piotr.roszatycki@gmail.com