Note that you can also await the same promise multiple times, so you could do it like this:
async function makePizza(sauceType = 'red') { let dough = makeDough(); let sauce = makeSauce(sauceType); let cheese = grateCheese((await sauce).determineCheese()); (await dough).add(await sauce); (await dough).add(await cheese); return (await dough); } Now the variables will be awaited on first use Marius Gundersen On 24 Feb 2017 03:36, "Šime Vidas" <sime.vi...@gmail.com> wrote: Daniel Brain from PayPal has written a post about async/await: https://medium.com/@bluepnume/even-with-async-await-you- probably-still-need-promises-9b259854c161 It revolves around writing an async function which would execute three tasks in parallel like so: |--------- dough ---------> |---- sauce ----> |-- cheese --> The code used as a starting point was: async function makePizza(sauceType = 'red') { let dough = await makeDough(); let sauce = await makeSauce(sauceType); let cheese = await grateCheese(sauce.determineCheese()); dough.add(sauce); dough.add(cheese); return dough; } This pattern, of course, cases the tasks to execute in sequence, like so: |-------- dough --------> |-------- sauce --------> |-- cheese --> The remainder of the post introduces several solutions in an attempt to achieve optimal concurrency. For instance, the author’s preferred solution uses a custom memoize function and Promise.all. Compared to the initial code above, these solutions seem complex, almost as if the language does not have the appropriate syntactic forms and/or APIs to address this particular use case. This got me thinking. What if there was a version of await that doesn’t pause execution on the spot, but continues execution until the variable which the await is assigned to, is first referenced? I’ve added comments to mark the positions where execution pauses: async function makePizza(sauceType = 'red') { let dough = await makeDough(); let sauce = await makeSauce(sauceType); let cheese = await grateCheese(/* pause to await sauce */ sauce.determineCheese()); /* pause to await dough */ dough.add(sauce); dough.add(/* pause to await cheese */ cheese); return dough; } Please let me know if adding a await-like keyword that works like this would be a bad idea. From a layman’s perspective, it seems that having this feature would simplify code patterns which involve execution in parallel like the example described in the article. _______________________________________________ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss