On Wed, Oct 9, 2019 at 11:17 PM Andrea Giammarchi
<[email protected]> wrote:
> > 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.

I think you're misreading my example?  Check this out:
http://software.hixie.ch/utilities/js/live-dom-viewer/saved/7271

```html
<!DOCTYPE html>
<script>
function one() {
  oneAsync();
  w("one A");
}
async function oneAsync() {
  await Promise.resolve();
  w("one B");
}

function two() {
  twoAsync();
  w("two A");
}

async function twoAsync() {
  // await? true;
  w("two B");
}

one();
two();
</script>
```

This script logs:

```
log: one A
log: two B
log: two A
log: one B
```

A and B are logged in different order depends on whether there's an
`await` creating a microtask checkpoint or not. `await? true` won't
create a microtask checkpoint, so you'll get the two() behavior,
printing B then A. `await? Promise.resolve(true)` will create one, so
you'll get the one() behavior, printing A then B.

> 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.

Ah, your earlier posts didn't say that `await? nonPromise` *would*
schedule a microtask in some cases! That does change things.  Hm, I
wonder if this is observably different from the current behavior?

~TJ
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to