Closed loveholly closed 1 week ago
Using .mjs
or ESM means you are already running inside a async
function (that's why top-level await is possible).
So, the order should not be same as CommonJS.
I'm not sure that makes sense - everything between start
and bar
is synchronous. The promise constructor runs synchronously. start foo
implies both nextTick runs synchronously and the promise constructor asynchronously - definitely an interesting issue!
Oh I was confused, I thought you called bar() inside the promise constructor rather than just resolving that promise.
Yeah - this makes sense an easier repro is:
Promise.resolve().then(() => {
console.log('promise callback');
});
process.nextTick(() => console.log('nextTick callback'));
This is the classic "order inside callback" gotcha (i.e. it's first tick weirdness, if you put it inside of a setImmediate it will log the same in both mjs and js).
This needs a docs fix
The docs are still wrong and need an update though :)
is this still open ?
I would like to add another point: if you use .mjs in a bun.sh
, you will get the same output as .cjs with the sample code above.
Version
v20.13.1
Platform
Darwin 23.3.0 Darwin Kernel Version 23.3.0
Subsystem
No response
What steps will reproduce the bug?
Use the code sample mentioned in the official documentation:
Save the code sample above to a file named
test.mjs
, and then execute it withnode test.mjs
. Here is the output fortest.mjs
:start bar foo zoo baz
. When I change the file totest.js
and re-run it, I will get the message with:start foo bar zoo baz
How often does it reproduce? Is there a required condition?
Always.
What is the expected behavior? Why is that the expected behavior?
It should print:
start foo bar zoo baz
What do you see instead?
It will print:
start bar foo zoo baz
Additional information
No response