Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@SamanShafigh
Created July 19, 2018 03:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SamanShafigh/bb39259da4aac10271cc9745d2b30afa to your computer and use it in GitHub Desktop.
Save SamanShafigh/bb39259da4aac10271cc9745d2b30afa to your computer and use it in GitHub Desktop.
async promise
// Async using promise
var addB = function (a, b) {
return new Promise((resolve, reject) => {
setTimeout (() => {
resolve(a + b);
}, 0);
});
};
// How to use it :D
addB(2, 3).then((result) => {
console.log(result);
});
// No Callback Hell :)
addB(1, 1)
.then((r) => addB(r, 1))
.then((r) => addB(r, 1))
.then((r) => addB(r, 1))
.then((r) => addB(r, 1))
.then((r) => addB(r, 1))
.then((r) => addB(r, 1))
.then((r) => {
console.log(r)
});
// You can also use Promise all to aggregate the result of multiple promise actions
var p1 = addB(2, 3)
var p2 = addB(2, 3)
var p3 = addB(2, 3)
Promise.all([p1, p2, p3]).then(([r1, r2, r3]) => {
console.log(r1, r2, r3)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment