tc39/proposal-top-level-await

Task queue completions before parent completions in v8

guybedford opened this issue · 3 comments

In the following test case:

a.js

console.log('a start');
await 0;
console.log('a end');
Promise.resolve().then(() => console.log('a after'));

b.js

import './a.js';
console.log('b start');

The execution in v8 follows:

a start
a end
a after
b start

Can we verify this is the expected order per this spec, including ensuring test262 coverage of the case?

The currently implemented ordering seems inefficient. There's no reason to delay evaluation of the b.js. Meaning ideal ordering would be:

a start
a end
a after
b start
sokra commented

I think it makes kind of sense if you see an async module similar to an async function.

With an async function there is also a micro-tick between the last line and any work that follows after that.

(async () => {
  console.log('a start');
  await 0;
  console.log('a end');
  Promise.resolve().then(() => console.log('a after'));
})().then(() => { console.log('next stuff'); });

Agreed the async wrapper analogy makes sense and fits intuition. The behaviour definitely matches the spec as well, I just found it a little surprising to see in these test cases. Closing.