promise-now
v1.1.0
Published
Barebone Promise/A+ implementation
Downloads
53
Readme
Promise Now
Barebone Promise/A+ implementation. .then()
being asynchronous is optional.
Installation
npm install promise-now
Example
var Promise = require('promise-now');
var promise = new Promise();
promise.then(addOne).then(addOne).then(function(num) {
console.log(num); // 3
});
promise.fulfill(1);
function addOne(num) {
return num + 1;
}
API
promise.then(fulfullCallack, rejectCallback);
See the Q tutorial, if you are not familiar with promises.
promise.fulfill(value);
Fullfil promise
with value
. This method only exists on the promises created by new Promise()
, not on promises returned by .then()
.
Returns promise
.
promise.reject(reason);
Reject promise
with reason
. This method only exists on the promises created by new Promise()
, not on promises returned by .then()
.
Returns promise
.
Optionally being asynchonous
If you can be sure that you will never write code like:
var promise = new Promise().fulfill();
promise.then(function() {
console.log(2);
});
console.log(1);
In other words, you will not put synchronous code after asynchronous function calls, 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, consider using another promise library, or patch promise-now youself (see test/promise.js
on how it's done).