Re: Would it be possible to add “await on first use” to the language?
Danielle McLean
gopsychonauts at gmail.com
Fri Feb 24 05:28:07 UTC 2017
On 24 February 2017 at 16:19:03, Šime Vidas (sime.vidas at gmail.com) wrote:
> To clarify, the idea is to declare and kick off all the concurrent tasks upfront
Well, that's what promises *already* do, even without using the
`async` and `await` keywords. You kick off all concurrent tasks
up-front - it's only tasks that depend on a previous task's result
that need wait around for that task to finish. Without `async`
functions, you'd probably do something like this:
function makePizza(sauceType = 'red') {
const dough = makeDough(), sauce = makeSauce(sauceType);
const cheese = sauce.then(s => grateCheese(s.determineCheese()));
return Promise.all([dough, sauce, cheese]).then(function([dough,
sauce, cheese]) {
dough.add(sauce);
dough.add(cheese);
return dough;
}
}
With `async` functions, you can avoid all those lambdas and the code's
a little bit cleaner - either way, you don't need new JS magic to do
things concurrently!
More information about the es-discuss
mailing list