Created
September 18, 2018 17:08
-
-
Save SergioGeeK7/b1ca6ec29ac21ff807b9a18686ed7888 to your computer and use it in GitHub Desktop.
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
let n = 0; | |
const pro = () => { | |
return Promise.resolve(++n); | |
}; | |
const stored = pro(); | |
stored.then(a => console.log(a)); | |
stored.then(a => console.log(a)); | |
stored.then(a => console.log(a)); | |
// --------------------- | |
let fooPromise = getFoo(); | |
let barPromise = getBar(); | |
let foo = await fooPromise; | |
let bar = await barPromise; | |
//This code again requires you to understand promises to figure out what’s happening. | |
//First. we dispatch `getFoo` and `getBar` and save the promises they return in `fooPromise` and `barPromise`. | |
//These actions are now in progress, they’re happening, there’s no stopping them or delaying them | |
//We await each promise in turn | |
//Doesn’t awaiting mean the two async actions are run in serial? | |
//Not so! In this case we’ve dispatched both async actions before we await anything, | |
//so they’re both run concurrently. By the time we start awaiting, it’s too late to delay either of them. |
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
async function makePizza(sauceType = 'red') { | |
let prepareDough = memoize(async () => makeDough()); | |
let prepareSauce = memoize(async () => makeSauce(sauceType)); | |
let prepareCheese = memoize(async () => { | |
return grateCheese((await prepareSauce()).determineCheese()); | |
}); | |
let [ dough, sauce, cheese ] = | |
await Promise.all([ | |
prepareDough(), prepareSauce(), prepareCheese() | |
]); | |
dough.add(sauce); | |
dough.add(cheese); | |
return dough; | |
} | |
// This is my favorite solution. Instead of setting up promises in advance, which are implicitly run concurrently, we’re setting up three memoized tasks | |
// (which are guaranteed to only be run one-time each), | |
// and invoking them all in Promise.all to be run concurrently. | |
//====== | |
async function makePizza(sauceType = 'red') { | |
let doughPromise = makeDough(); | |
let saucePromise = makeSauce(sauceType); | |
let sauce = await saucePromise; | |
let cheese = await grateCheese(sauce.determineCheese()); | |
let dough = await doughPromise; | |
dough.add(sauce); | |
dough.add(cheese); | |
return dough; | |
} | |
// | |
function makePizza(sauceType = 'red') { | |
let doughPromise = makeDough(); | |
let saucePromise = makeSauce(sauceType); | |
let cheesePromise = saucePromise.then(sauce => { | |
return grateCheese(sauce.determineCheese()); | |
}); | |
return Promise.all([ doughPromise, saucePromise, cheesePromise ]) | |
.then(([ dough, sauce, cheese ]) => { | |
dough.add(sauce); | |
dough.add(cheese); | |
return dough; | |
}); | |
} | |
// | |
async function makePizza(sauceType = 'red') { | |
let [ dough, sauce ] = | |
await Promise.all([ makeDough(), makeSauce(sauceType) ]); | |
let cheese = await grateCheese(sauce.determineCheese()); | |
dough.add(sauce); | |
dough.add(cheese); | |
return dough; | |
} |
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
const test1 = async () => { | |
const time1 = new Date().getTime(); | |
await Promise.delay(900); | |
await Promise.delay(1200); | |
await Promise.delay(1600); | |
const time2 = new Date().getTime(); | |
console.log('result 1: ', time2 - time1); | |
}; | |
const test2 = async () => { | |
const time1 = new Date().getTime(); | |
await Promise.all([Promise.delay(900), Promise.delay(1200), Promise.delay(1600)]); | |
const time2 = new Date().getTime(); | |
console.log('result 2: ', time2 - time1); | |
}; | |
test1(); | |
test2(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment