-
Notifications
You must be signed in to change notification settings - Fork 29.7k
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
doc: fix process.nextTick() scheduling differences between module types #45093
base: main
Are you sure you want to change the base?
Conversation
@@ -2515,7 +2515,8 @@ The [`queueMicrotask()`][] API is an alternative to `process.nextTick()` that | |||
also defers execution of a function using the same microtask queue used to | |||
execute the then, catch, and finally handlers of resolved promises. Within | |||
Node.js, every time the "next tick queue" is drained, the microtask queue | |||
is drained immediately after. | |||
is drained immediately after. Note that `process.nextTick()` is scheduled |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
process.nextTick
is not scheduled differently, the module's execution is scheduled differently (specifically, it is a microtask). I think we should be careful of our wording here so people don't get confused.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree. The wording here is a bit off. I would suggest wording like, "The timing of the execution of tasks scheduled with process.nextTick()
will vary depending on the module type being used."
I think it is because the |
This code imports the nextTick function from the built-in Node.js process module and then creates a chain of asynchronous operations using Promise.resolve(), queueMicrotask(), and nextTick(). Here's a breakdown of what each part of the code does: The Promise.resolve().then(() => console.log(2)) creates a resolved promise and schedules a callback to be executed when the promise is resolved. In this case, the callback logs the number 2 to the console. The queueMicrotask(() => console.log(3)) adds a function to the microtask queue, which will be executed after the current task has finished executing. In this case, the function logs the number 3 to the console. The nextTick(() => console.log(1)) schedules a callback to be executed on the next iteration of the event loop. In this case, the callback logs the number 1 to the console. Since the callbacks are executed asynchronously, the order in which the numbers are logged to the console may not be what you expect. Here is the expected output: BUT The reason why the output is 2 3 1 instead of 1 2 3 is that the nextTick() function and queueMicrotask() function are similar but have slightly different behavior. In this case, the queueMicrotask() callback is executed before the nextTick() callback, which causes the output to be in the order of 2 3 1. The nextTick() function schedules a callback to be executed on the next iteration of the event loop, but after I/O operations have been processed. The queueMicrotask() function, on the other hand, schedules a callback to be executed on the microtask queue, which is executed before the next event loop iteration. In this specific case, it's possible that the nextTick() callback was delayed due to I/O operations, which allowed the queueMicrotask() callback to be executed first. This behavior can vary depending on the environment and platform that the code is running on, so it's not always predictable which callback will be executed first. In general, when scheduling multiple asynchronous operations, it's important to be aware of the differences between the various scheduling functions and their behavior in different contexts. @Trott @dnalborczyk @devsnek @theanarkh If you Guys want I can do a PR |
@parmishh the issue is that running the same code differs in behavior when run as a Anyhow, what's needed is a short description why the example code differs in both module systems.
If I'm not mistaken, a Promise is actually going into the same |
I had to crawl the issues for some time to understand that the esm/cjs divergence was not a bug. |
what is blocking this PR? |
This issue/PR was marked as stalled, it will be automatically closed in 30 days. If it should remain open, please leave a comment explaining why it should remain open. |
fixes #45048