promise-extended
promise-extended
is a Javascript library that can be used standalone or incorporated into extended
var promise = ;
Or
var myextended = ;
Installation
npm install promise-extended
Or download the source (minified)
Usage
Promise
The promise constructor used by promise
and defer
Promise#callback
Resolve a promise with the given value.
Promise#errback
Reject a promise with the given value.
Promise#callback
Resolve a promise with the given value.
Promise#then(callback[, errback)
Allows the chaining of promises.
function myAsyncAction(num){
var p = new promise.Promise();
setTimeout(funciton(){
p.callback(num);
}, 50);
return p.promise();
}
myAsyncAction(1)
.then(function(num){
return myAsyncAction(num+1);
})
.then(function(num){
return myAsyncAction(num+1);
})
.then(function(num){
console.log(num); //3
});
Errors are propagated the the nearest error handler, this allows the bubbling of errors up a chain of promises.
function myAsyncAction(num){
var p = new promise.Promise();
setTimeout(funciton(){
p.callback(num);
}, 50);
return p.promise();
}
myAsyncAction(1)
.then(function(num){
throw new Error("oops an error occured");
})
.then(function(num){
return myAsyncAction(num+1);
})
.then(
function(num){
console.log(num); //3
},
function(err){
console.log(err); //oops an error occured.
}
);
Of you return a promise from an error handler that resolved successfully then the next promise is the chain will be resolved successfully.
function myAsyncAction(num){
var p = new promise.Promise();
setTimeout(funciton(){
p.callback(num);
}, 50);
return p.promise();
}
myAsyncAction(1)
.then(function(num){
throw new Error("oops an error occured");
})
.then(function(num){
return myAsyncAction(num+1);
})
.then(
function(num){
console.log(num); //3
},
function(err){
console.log(err); //oops an error occured.
return asyncAction(10);
}
)
.then(function(num){
console.log(num); //10
});
Promise#both
Allows the execution of a function regardless of whether or not a promises is successful.
new promise.Promise().errback("error").both(function(){
//do some more work
}).then(function(){
});
Promise#resolve
The resolve method provides a mechanism to work with node style callbacks and promises.
var fs = ; var myPromise = ;fs; myPromise;
Promise#classic
Accepts a callback in the typical node style.
new promise.Promise()
.callback("hello")
.classic(function(err, res){
console.log(res); //"hello"
});
isPromiseLike
Determines if an object is like a promise (contains a .then
function).
promise; //true; //true
promise
Creates a new promise.
{ var p = promise; ; return p;} ;
promiseList
Creates a new promise.
{ var p = promise; ; return p;} promise;
defer
Creates a new promise.
{ var p = promisedefer; ; return p;} myAsyncAction;
deferredList
Creates a new promise.
{ var p = promisedefer; ; return p;} promise;
resolve
Creates a promise that is resolved with the provided value.
promise ;
reject
Creates a promise that is errored with the provided value.
promise ;
wrap
Wraps traditional node style functions with a promise.
var fs = ;var readFile = promise;;
serial
Executes a list of items in a serial manner.
If the list contains promises then each promise will be executed in a serial manner.
If the list contains non async items then the next item in the list is called.
Note This will not propogate values from one action to another, instead results are passed as an array to the eventual promise.
{ return { var p = promise; ; return p; }}; promise;
chain
Allows you to propogate results from one function to another.
This is different than .serial
in that it propogates results from one promise to the next, where .serial
does not.
{ return { num = num || 0; var p = promise; ; return p;; }} promise;
wait
Ensures that a promise is resolved before a the function can be run.
For example suppose you have to ensure that you are connected to a database before you execute a function.
var findUser = promise.wait(connect(), function findUser(id){
//this wont execute until we are connected
return User.findById(id);
});
promise.when(findUser(1), findUser(2)).then(function(users){
var user1 = users[0], user2 = users[1];
});