> What's the order of the logs?

Exactly the same, as the `await?` is inevitably inside an `async` function
which would grant a single microtask instead of N.

Example:

```js
async function tasks() {
  await? maybeAsync();
  await? maybeAsync();
  await? maybeAsync();
}

tasks();
```

If `maybeAsync` returns twice non promises results, there is only one
microtask within the async `tasks` function, that would linearly collect
all non promises, so that above example could have 1, 2, max 4 microtasks,
instead of always 4
.
To explain `await?` in steps:

  * is the awaited value a promise? schedule microtask
  * otherwise schedule a single microtask if none has been scheduled
already, or queue this result to the previous scheduled one

This would grant same linear order and save time.

However, like others said already in the twitter thread, we all wish
`await` was already working like that by default, while it seems to
unconditionally create micro tasks even when it's not strictly necessary.




On Wed, Oct 9, 2019 at 11:47 PM Tab Atkins Jr. <jackalm...@gmail.com> wrote:

> On Wed, Oct 9, 2019 at 12:08 AM Andrea Giammarchi
> <andrea.giammar...@gmail.com> wrote:
> > I don't know why this went in a completely unrelated direction so ...
> I'll try to explain again what is `await?` about.
>
> Nah, we got it. Our complaint was still about the semantics.
>
> > ```js
> > const value = await? callback();
> >
> > // as sugar for
> > let value = callback();
> > if ('then' in value)
> >   value = await value;
> > ```
> >
> > The order is guaranteed and linear in every case, so that nothing
> actually change logically speaking, and the hint would be about
> performance, 'cause engines don't apparently optimize non-promise based
> cases.
>
> Expand that code so there's a caller:
>
> ```js
> function one() {
>   two();
>   console.log("one");
> }
> async function two() {
>   await? maybeAsync();
>   console.log("two");
> }
> ```
>
> What's the order of the logs?
>
> If maybeAsync() is synchronous, then one() calls two(), two() calls
> maybeAsync() which returns immediately, and it continues to log "two"
> before ending and returning execution to one(), which then logs "one"
> and ends.
>
> If maybeAsync() returns a promise, then one() calls two(), two calls
> maybeAsync() then freezes while it waits for the promise to resolve,
> returning execution to one(). Since one() isn't awaiting the promise
> returned by two(), it just immediately continues and logs "one" then
> ends. At some point later in execution, the maybeAsync() promise
> resolves, two() unfreezes, then it logs "two".
>
> So no, the order is not guaranteed. It's unpredictable and depends on
> whether the function being await?'d returns a promise or not. If you
> don't know what maybeAsync() returns, you won't be able to predict
> your own execution flow, which is dangerous and a very likely source
> of bugs.
>
> ~TJ
>
_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to