promise-now
Barebone Promise/A+ implementation.
Features
-
Extremely small (~ 1kb minified), extremely fast
-
.then()
being asynchronous is optional. -
Passing the Promises/A+ Compliance Test Suite
Installation
npm install promise-now
Example
var Promise = ;var promise = ; promise;promise; { return num + 1;}
API
promise.then([fulfillCallack], [rejectCallback]);
Call fulfillCallack(value)
if promise
is fulfilled with value
.
Or call rejectCallback(reason)
if promise
is rejected with reason
.
Return a new promise.
promise.fulfill(value, [context]);
Fulfill promise
with value
. this
keyword equals to context
in callbacks if provided.
Return original promise
.
promise.reject(reason, [context]);
Reject promise
with reason
. this
keyword equals to context
in callbacks if provided.
Return original promise
.
promise.done([fulfillCallack], [rejectCallback]);
Like .then()
, but throw with reason (asynchronously) if promise is rejected. Should be called at the end of .then()
chain.
Return undefined
.
.then() being asynchronous
If you can be sure that you will never write code like:
var promise = ; promise;console;
In other words, you will not put synchronous code after asynchronous function calls, then it doesn't make a difference if .then()
is asynchronous or not.
By default, promise-now use synchronous .then()
. If you need the asynchronous version, simply patch promise-now (see test/promise.js
on how it's done).