Created
May 17, 2019 18:15
-
-
Save javanigus/09440424b8161c99e79657bde325a2ea to your computer and use it in GitHub Desktop.
Promise.all example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// first we need an asynchronous function that returns a promise | |
function getBurger () { | |
return new Promise(function (resolve, reject) { | |
// making burger ... | |
if (error) { | |
reject(error); | |
} else { | |
resolve(burger); | |
} | |
}); | |
} | |
// next, let’s order some burgers | |
p1 = getBurger(); | |
p2 = getBurger(withCheese); | |
p3 = getBurger(withCheese, noOnion); | |
// collect all the receipts | |
receipts = new Array(); | |
receipts.push(p1); | |
receipts.push(p2); | |
receipts.push(p3); | |
// define what we will do once everyone gets a burger | |
function everyoneCanEat () {} | |
// define what we will do if anyone doesn’t get a burger | |
function somethingWentWrong () {} | |
// tell the Promise class you want to wait for ALL the these receipts to be resolved (or rejected) | |
Promise.all(receipts).then(everyoneCanEat).catch(somethingWentWrong); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment