Skip to content

Commit 511d89a

Browse files
fix(learn): improve setImmediate in Promises article (#7677)
* fix: setImmediate docs * fix: make setImmediate docs more beginner-friendly. * fix: format * fix: format * fix: drop 'immediately' * fix: correct event loop terminology * docs: link event loop
1 parent 88aaaca commit 511d89a

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

apps/site/pages/en/learn/asynchronous-work/discover-promises-in-nodejs.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ console.log('Synchronous task executed');
367367

368368
### `setImmediate()`
369369

370-
`setImmediate()` is used to execute a callback after the current event loop cycle finishes and all I/O events have been processed. This means that `setImmediate()` callbacks run after any I/O callbacks, but before timers.
370+
`setImmediate()` schedules a callback to be executed in the check phase of the Node.js [event loop](https://nodejs.org/en/learn/asynchronous-work/event-loop-timers-and-nexttick), which runs after the poll phase, where most I/O callbacks are processed.
371371

372372
```js
373373
setImmediate(() => {
@@ -381,7 +381,7 @@ console.log('Synchronous task executed');
381381

382382
- Use `queueMicrotask()` for tasks that need to run immediately after the current script and before any I/O or timer callbacks, typically for Promise resolutions.
383383
- Use `process.nextTick()` for tasks that should execute before any I/O events, often useful for deferring operations or handling errors synchronously.
384-
- Use `setImmediate()` for tasks that should run after I/O events but before timers.
384+
- Use `setImmediate()` for tasks that should run after the poll phase, once most I/O callbacks have been processed.
385385

386386
Because these tasks execute outside of the current synchronous flow, uncaught exceptions inside these callbacks won't be caught by surrounding `try/catch` blocks and may crash the application if not properly managed (e.g., by attaching `.catch()` to Promises or using global error handlers like `process.on('uncaughtException')`).
387387

0 commit comments

Comments
 (0)