eprom
v1.0.0
Published
JavaScript Async Primitive
Downloads
902
Keywords
Readme
eprom
eprom
is an enhanced promise implementation. It works by wrapping a globally available Promise
class and is as compliant with the Promises/A+ spec as the global Promise
is.
In addition to the usual then
, call
and finally
instance methods, it provides resolve
and reject
methods. In this regard, it resembles jQuery's Deferred
object. On top of that, it features a reset
method that enables repeat fulfillment.
Installation
Using NPM:
npm install -S eprom
Using Yarn:
yarn add eprom
API
eprom
's EnhancedPromise
mimics more usual Promises
in every way. As such, it provides all class (resolve
, reject
, all
, race
) and instance (then
, catch
, finally
) methods these provide.
enhancedPromise.resolve(value)
This method resolves an EnhancedPromise
's inner Promise
, triggering the execution of all onFulfilled
handlers.
const enhancedPromise = new EnhancedPromise();
enhancedPromise.then(value => console.log(value));
enhancedPromise.resolve('foo');
// logs 'foo'
enhancedPromise.reject(reason)
This method rejects an EnhancedPromise
's inner Promise
, triggering the execution of all onRejected
handlers.
const enhancedPromise = new EnhancedPromise();
enhancedPromise.catch(reason => console.err(reason));
enhancedPromise.reject('bar');
// logs 'bar'
enhancedPromise.reset()
This method creates a fresh inner Promise
and thus allows for the re-fulfillment of an EnhancedPromise
. A typical use case for this is handling repeat builds triggered by Webpack in watch mode.
const enhancedPromise = new EnhancedPromise();
enhancedPromise.then(value => console.log(value));
enhancedPromise.resolve('foo');
// logs 'foo'
enhancedPromise.reset();
enhancedPromise.then(value => console.log(value));
enhancedPromise.resolve('bar');
// logs 'bar'