Skip to content

Commit

Permalink
fix: correct the execution order in esm (nodejs#6725)
Browse files Browse the repository at this point in the history
  • Loading branch information
jazelly authored May 22, 2024
1 parent 9ad9238 commit 5d5b626
Showing 1 changed file with 8 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,11 @@ start();
```

This code will first call `start()`, then call `foo()` in `process.nextTick queue`. After that, it will handle `promises microtask queue`, which prints `bar` and adds `zoo()` in `process.nextTick queue` at the same time. Then it will call `zoo()` which has just been added. In the end, the `baz()` in `macrotask queue` is called.

The principle aforementioned holds true in CommonJS cases, but keep in mind in ES Modules, e.g. `mjs` files, the execution order will be different:

```js
// start bar foo zoo baz
```

This is because the ES Module being loaded is wrapped as an asynchronous operation, and thus the entire script is actually already in the `promises microtask queue`. So when the promise is immediately resolved, its callback is appended to the `microtask` queue. Node.js will attempt to clear the queue until moving to any other queue, and hence you will see it outputs `bar` first.

0 comments on commit 5d5b626

Please sign in to comment.