Deferred
Tiny extension for native Promises to make them more suitable for Node core APIs.
The package exposes a subclass of the built-in Promise
, which extends that with the following four methods.
new Deferred([executor])
Unlike Promise
, Deferred
does not require an executor function for instantiation, because it makes possible to
resolve or reject the instance from outside the executor's scope.
const d1 = d2 = executor d1 { // same as d2.reject(...)} d1d2
Output:
success: sun is shining
error: baby is crying
deferred.resolve(value)
Resolve the deferred instance.
deferred.reject(reason)
Reject the deferred instance.
deferred.done(callback)
Attach an error-first callback to the instance. It will be fired once the instance resolves or rejects.
const d1 = d2 = { if err console else console} d1d2 d1d2
Output:
success: your pizza order has arrived
error: something exploded
deferred.callback()
Generate an error-first callback which will resolve or reject the instance based on the first parameter passed to it.
const d1 = d2 = cb1 = d1 cb2 = d2 d1d2
Output:
success: your cat is purring
error: coffee machine is out of order
Installation
With npm:
npm install defd
Tests
Run unit tests:
npm test
Run unit tests and create coverage report:
npm run cover