Skip to content

Commit bb2b6d4

Browse files
authored
docs: update explanation of setImmediate vs setTimeout
Clarifies the execution order of setTimeout and setImmediate when called from the main module and inside an I/O callback. Signed-off-by: Sébastien Dan <sebastien.dan@ikmail.com>
1 parent d347f7b commit bb2b6d4

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

apps/site/pages/en/learn/asynchronous-work/understanding-setimmediate.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ How is `setImmediate()` different from `setTimeout(() => {}, 0)` (passing a 0ms
2020

2121
A function passed to `process.nextTick()` is going to be executed on the current iteration of the event loop, after the current operation ends. This means it will always execute before `setTimeout` and `setImmediate`.
2222

23-
A `setTimeout()` callback with a 0ms delay is very similar to `setImmediate()`. The execution order will depend on various factors, but they will be both run in the next iteration of the event loop.
23+
A `setTimeout()` callback with a 0ms delay is very similar to `setImmediate()`. The execution order will depend on various factors, but they will be both run in the next iteration (the first one) of the event loop when called from the main module. When scheduled inside an I/O callback, setImmediate is guaranteed to run in the current iteration's Check phase, while setTimeout must wait for the Timers phase of the subsequent iteration.
2424

2525
A `process.nextTick` callback is added to `process.nextTick queue`. A `Promise.then()` callback is added to `promises microtask queue`. A `setTimeout`, `setImmediate` callback is added to `macrotask queue`.
2626

0 commit comments

Comments
 (0)