Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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