promise-now

Barebone Promise/A+ implementation

Promise Now

Barebone Promise/A+ implementation. .then() being asynchronous is optional.

npm install promise-now
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;
}
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.

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).