Solution to "callback hell"
Common Promise example
Basics
Basics
Basics
Full list
Basics
Will always be Async
Executed at least on the next tick
a Promise handler always return another Promise
Promises can be used only once
a Promise object is "Then-able"
Basics
Basics
Use
getUser('userID')
.then(onFulfilled, onRejected)
getUser('userID')
.then(function(userData) {
if(userData) return 'Success!'
else throw new Error('No user data was found...')
//your app logic error, always throw with an Error...
},
function(reason){
//handle DB errors...
})
Use
"a Promise handler always return another Promise"
therefore it can be chained to other Promises
Use
function readFile(filename, enc){
return new Promise(function (fulfill, reject){
fs.readFile(filename, enc, function (err, res){
if (err) reject(err);
else fulfill(res);
});
});
}
Create
var readFile = Promise.promisify(require("fs").readFile);
readFile("myfile.js", "utf8").then(function(contents) {
return eval(contents);
}).then(function(result) {
console.log("The result of evaluating myfile.js", result);
}).catch(SyntaxError, function(e) {
console.log("File had syntax error", e);
//Catch any other error
}).catch(function(e) {
console.log("Error reading file", e);
});
Create
var fs = Promise.promisifyAll(require("fs"));
fs.readFileAsync("myfile.js", "utf8").then(function(contents) {
console.log(contents);
}).catch(function(e) {
console.error(e.stack);
});
Create
fetch('/api/friends')
.then(promisedStep1)
.then(promisedStep2)
.then(promisedStep3)
.then(promisedStep4)
.catch(function (error) {
// Handle any error from all above steps
})
Flow
var items = [0,1,2,3,4,5,6,7,8,9];
Promise.each(items, function(item) {
return Promise.delay(Math.random() * 2000)
.then(function() {
console.log('async Operation Finished. item:', item);
});
}).then(function(resultsArray) {
console.log('All tasks are done now...',resultsArray);
}).catch(function(error) {
console.dir(error)
});
Flow
Serial iterator over a collection
and then...
var items = [0,1,2,3,4,5,6,7,8,9];
Promise.map(items,function(item) {
return Promise.delay(Math.random() * 2000)
.then(function() {
console.log('async Operation Finished. item:', item);
});
}, {concurrency: 3}) //adjust concurrency...
.then(function() {
console.log('All tasks are done now...');
})
.catch(function(error) {
console.dir(error)
});
Flow
Parallel iterator over a collection
and then...
Promise.all(pendingItemsArray).then(function (items) {
console.log('All operations are complete!', 'items: ',items);
})
.catch(function (error) {
console.log('Error: ',error);
});
Flow
Parallel execution of different async operations
and then...
Promise.props(pendingItemsObj).then(function(result) {
console.log('All operations are complete!', 'result: ');
console.dir(result);
})
.catch(function (error) {
console.log('Error: '.red,error);
})
Flow
Parallel execution of different async operations
and then...
Promise.race(pendingItemsArray).then(function (item) {
console.log('The first operation is complete!', 'first item: ',item);
})
.catch(function (error) {
console.log('Error: '.red,error);
});
Flow
The .then( ) handler runs after the
first async operation is complete
Promise.some(pendingItemsArray, 2)
.spread(function(first, second) {
console.log('first: ', first, ', ','second: ', second);
})
.catch(function (error) {
console.log('Error: ',error);
});
Flow
Similar to race()
The .then() handler runs after the (n)th async operation is complete