Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nextTick doesn't execute as expected with .mjs #52978

Open
loveholly opened this issue May 14, 2024 · 7 comments · Fixed by nodejs/nodejs.org#6725
Open

nextTick doesn't execute as expected with .mjs #52978

loveholly opened this issue May 14, 2024 · 7 comments · Fixed by nodejs/nodejs.org#6725
Labels
doc Issues and PRs related to the documentations. good first issue Issues that are suitable for first-time contributors.

Comments

@loveholly
Copy link

loveholly commented May 14, 2024

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:

const baz = () => console.log('baz');
const foo = () => console.log('foo');
const zoo = () => console.log('zoo');

const start = () => {
  console.log('start');
  setImmediate(baz);
  new Promise((resolve, reject) => {
    resolve('bar');
  }).then(resolve => {
    console.log(resolve);
    process.nextTick(zoo);
  });
  process.nextTick(foo);
};

start();

// start foo bar zoo baz

Save the code sample above to a file named test.mjs, and then execute it with node test.mjs.
Here is the output for test.mjs:
start bar foo zoo baz.
When I change the file to test.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

@loveholly loveholly changed the title nextTick not executed as expected with .mjs nextTick doesn't execut as expected with .mjs May 14, 2024
@loveholly loveholly changed the title nextTick doesn't execut as expected with .mjs nextTick doesn't execute as expected with .mjs May 14, 2024
@climba03003
Copy link
Contributor

climba03003 commented May 14, 2024

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.

@benjamingr

This comment was marked as resolved.

@benjamingr
Copy link
Member

Oh I was confused, I thought you called bar() inside the promise constructor rather than just resolving that promise.

@benjamingr
Copy link
Member

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

@benjamingr benjamingr added the doc Issues and PRs related to the documentations. label May 14, 2024
@benjamingr
Copy link
Member

The docs are still wrong and need an update though :)

@benjamingr benjamingr reopened this May 15, 2024
@benjamingr benjamingr added the good first issue Issues that are suitable for first-time contributors. label May 15, 2024
@Naveen-2021ucp1387
Copy link

is this still open ?

@loveholly
Copy link
Author

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.

jazelly added a commit to jazelly/nodejs.org that referenced this issue May 16, 2024
jazelly added a commit to jazelly/nodejs.org that referenced this issue May 16, 2024
jazelly added a commit to jazelly/nodejs.org that referenced this issue May 16, 2024
github-merge-queue bot pushed a commit to nodejs/nodejs.org that referenced this issue May 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Issues and PRs related to the documentations. good first issue Issues that are suitable for first-time contributors.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants