A bare bones implementation of Promises/A intended to pass https://github.com/domenic/promise-tests while being as small as possible
A bare bones implementation of Promises/A intended to pass all promise-tests while being as small as possible.
This is intended to serve as a base for you to build your own promises libraries, which would typically be expected to provide all sorts of useful helpers. Promises created by this library (or any library based off of this) should be compatable with such helpers, but helpers won't ever be included in this library.
$ component install ForbesLindesay/promises-a
$ npm install promises-a
Alternatively you can download it from the downloads area and reference it with a script tag:
then just refer to it as the global:
Here is an example of how to create a promises function to wrap a low level api, and provide a timeout
var promise = require'promises-a';timeout = timeout == null ? 500 : timeout;var def = promise;callLowLevelAPIidif err return defrejecterr;defresolveres;;if timeoutsetTimeoutdefreject'Operation Timed Out (' + timeout + 'ms)';timeout;return defpromise;
Because the promise can only be resolved once, the rejection will be ignored if the operation is successful within the timeout. A timeout of 0 will also be treated as infinite.
Return a new
Get the promise represented by the deferred. This is just an object with a function called then.
Put the promise in a resolved state and fulfill it with the value provided.
Put the promise in a resolved state and reject it with the error provided.
You can call then with tow optional args. The callback is called when the promise is fulfilled, the errback is called when the promise is rejected. Then also returns a fresh promise which is set to the result of the callback or errback. If you want to forward a rejection either make errback
null, or re-throw the error in errback.
If you return a promise from callback or errback it will be resolved before being set as the result of the promise.
Equivalent to calling then, except you don't get a new promise out and exceptions are never swallowed.