Blue bird is a great library of promise for front end (angular) and backend (node). But its documentation is definitely not good. Let's list and explain shorty the difference between each useful functions this library provide.
Sequential processing means each promises is processed one after the other. Could be useful if you want to have a control on the flow of processing. Or if you need the result of previous promise to process the next.
Parallel processing means the promises are processed at the same time. You cannot guaranty that the order will be preserved.
## Useful bluebird function
* **Promise.all**
- Params: Array of promise
- Process in parallel
- Reject early if an error occur
- the return value is an array of each promises return in the order they were define
* **Promise.each**
Same than promise.all but
- Process in sequence
* **Promise.join**
It is recommended to use Promise.join instead or Promise.all
when the action to do are fixed in advance and not numerous.
In this case only 3 promises that we know in advance.
```js
var Promise = require("bluebird");
var join = Promise.join;
join(getPictures(), getComments(), getTweets(),
function(pictures, comments, tweets) {
console.log("in total: " + pictures.length + comments.length + tweets.length);
});
Use the properties of an object to define promise to be performed. The advantage is that the result is accessible with the same properties name that the input object. Giving the benefit of the spread() operator (instead of the then())
Promise.all([/* Array of promises */]).then(() => {// To do when all promises array resolve}).catch(() => {// To do if any of the promise in the array had arrors}).finally(() => {// To do after the then() or catch() indepedantly of the Promise faith (success or errors)})
cancel() Cancel a promise if it has not been received already
spread() The result of promise.all is an array of promises returns, but what if you want to define them as parameters ?
let arrayOfPromises = [ promise1, promise2 ];Promise.all(arrayOfPromises).spread((returnOfPromise1, returnOfPromise2) => {console.log("return of promise1 ", returnOfPromise1);console.log("return of promise2 ", returnOfPromise1); });// Instead ofPromise.all(arrayOfPromises).then((result) => {console.log("return of promise1 ", result[0]);console.log("return of promise2 ", result[2]); });
Allow doing something globally when error occurred
// NOTE: event name is camelCase as per node conventionprocess.on("unhandledRejection",function(reason, promise) {// See Promise.onPossiblyUnhandledRejection for parameter documentation });// NOTE: event name is camelCase as per node conventionprocess.on("rejectionHandled",function(promise) {// See Promise.onUnhandledRejectionHandled for parameter documentation });