How about awaiting Arrays ?

Gil Tayar gil at tayar.org
Sun Mar 5 06:34:42 UTC 2017


As someone who teaches JavaScript a lot, I believe that not explaining what
promises are when teaching async/await is not possible, because await only
works on *promises*. You need to at least understand that promises are
"async values" to understand and use aysnc/await. What is abstracted away
is the use of chainable promises and .then/.catch, and not the promises
themselves.

So saying that Promise.all accepts an array of promises and returns a
promise that is resolved only when they all resolve is not leaking the
async/await abstraction. It's still "OK".

There are hundreds of little syntax sugars we can add to the language. We
should choose carefully because each one makes the language more
complicated. Abstracting away promise chaining with async/await is a game
changer, and is worth the "syntactic sugar" - even more so IMHO than class
abstracted away prototype (which is a much leakier "syntax sugar").
Abstracting away Promise.all with syntactic sugar gives us little gain, and
in my opinion is not worth it.

- Gil


On Sat, Mar 4, 2017 at 5:03 PM Matthew Robb <matthewwrobb at gmail.com> wrote:

> Honestly Isiah my largest motivation for thinking this is worth solving
> boils down to the fact that async/await does a good job of hiding the
> promise and generator based implementation under it but this falls down so
> fast when adding Promise.all.
>
> I'm helping a new person learn JavaScript right now and he's using fetch
> to get some JSON. Explaining the first then returning res.json() and the
> second one chaining as well as 'this' considerations was a disaster. He
> conceptually understands asynchronous code so when I backed up and did the
> same thing with async/await he just got it. Saving him from needing to
> learn anything about promises until later on.
>
> In my opinion if a layer of sugar doesn't fully abstract the layers it
> sits upon then it's an incomplete and confusing feature. I'd go so far as
> to say that async/await should always support every capability of promise
> without anyone touching Promise directly.
>
> On Mar 3, 2017 9:12 PM, "Isiah Meadows" <isiahmeadows at gmail.com> wrote:
>
> First, I'll start out with this: I tend to be very adverse to new syntax,
> but I'll draw exceptions for things that enable whole new ways of looking
> at and manipulating code, like async functions and decorators, or things
> that enable new functionality altogether, like `function.sent` or private
> class fields. Things that haven't hit that bar for me include the bind
> syntax proposal (beyond function pipelining) and the
> `await.all`/`await.race` idea here.
>
> BTW, I had some ideas on unifying that with observables at the syntax
> level here, particularly with my `parallel` and `await parallel` ideas
> there: https://github.com/tc39/proposal-observable/issues/141
>
> Basically, it unifies the common case of merging promises and observables
> with a fairly low syntactic footprint.
>
> As for `Promise.race`, I see it much less frequently, and it's much
> simpler and faster under the hood to implement due to less state needed, so
> I didn't see the need to add support for that.
>
>
> -----
>
> Isiah Meadows
> me at isiahmeadows.com
>
> On Fri, Mar 3, 2017 at 11:59 AM, Michał Wadas <michalwadas at gmail.com>
> wrote:
>
> My mistake Array.from is not necessary because Promise.all accepts any
> iterable.
>
> Though I don't believe .race to be common use case (especially when we
> consider it's edge case with empty array).
>
> Hsving parity with spread and rest parameters seems consistent for me.
>
> On 3 Mar 2017 17:54, "T.J. Crowder" <tj.crowder at farsightsoftware.com>
> wrote:
>
> On Fri, Mar 3, 2017 at 4:51 PM, Michał Wadas <michalwadas at gmail.com>
> wrote:
> > Actually I would go with
> >
> > await ...expr;
> >
> > As sugar for:
> >
> > await Promise.all(Array.from(expr))
>
> Which is great for `Promise.all`, but leaves us without `race` or things
> that may be added in future (like Andrea's `any`). (Granted `all` has to be
> the dominant use case...)
>
> Why the `Array.from` part of that?
>
> -- T.J.
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20170305/25b7a6b8/attachment-0001.html>


More information about the es-discuss mailing list