Skip to content

Commit dc189d6

Browse files
fix: make setImmediate docs more beginner-friendly.
1 parent 35d7a18 commit dc189d6

File tree

1 file changed

+2
-12
lines changed

1 file changed

+2
-12
lines changed

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

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

368368
### `setImmediate()`
369369

370-
`setImmediate()` schedules its callback for the event loop’s check phase—i.e. it will run immediately after all of the current loops I/O callbacks (poll phase) have completed.
370+
`setImmediate()` is used to execute a callback immediately after all of the current loop's I/O callbacks have completed.
371371

372372
```js
373-
const fs = require('fs');
374-
375373
setImmediate(() => {
376374
console.log('Immediate callback');
377375
});
378376

379-
fs.readFile('./some.txt', () => {
380-
console.log('I/O callback (file read)');
381-
});
382-
383-
setTimeout(() => {
384-
console.log('Timeout callback')
385-
}, 0)
386-
387377
console.log('Synchronous task executed');
388378
```
389379

390380
### When to Use Each
391381

392382
- 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.
393383
- Use `process.nextTick()` for tasks that should execute before any I/O events, often useful for deferring operations or handling errors synchronously.
394-
– Use `setImmediate()` for tasks that should run after the current event loops I/O events, but before timers scheduled for the next loop iteration.
384+
– Use `setImmediate()` for tasks that should run after the current event loop's I/O events.
395385

396386
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')`).
397387

0 commit comments

Comments
 (0)