From 35d7a18bd7f4f0ce50c5e20c4e68b0e8a127c59e Mon Sep 17 00:00:00 2001 From: khappa Date: Wed, 23 Apr 2025 21:32:56 +0400 Subject: [PATCH 1/4] fix: setImmediate docs --- .../discover-promises-in-nodejs.md | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/apps/site/pages/en/learn/asynchronous-work/discover-promises-in-nodejs.md b/apps/site/pages/en/learn/asynchronous-work/discover-promises-in-nodejs.md index 363a9fc1e935d..ea94be600ee30 100644 --- a/apps/site/pages/en/learn/asynchronous-work/discover-promises-in-nodejs.md +++ b/apps/site/pages/en/learn/asynchronous-work/discover-promises-in-nodejs.md @@ -367,13 +367,23 @@ console.log('Synchronous task executed'); ### `setImmediate()` -`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. +`setImmediate()` schedules its callback for the event loop’s check phase—i.e. it will run immediately after all of the current loop’s I/O callbacks (poll phase) have completed. ```js +const fs = require('fs'); + setImmediate(() => { console.log('Immediate callback'); }); +fs.readFile('./some.txt', () => { + console.log('I/O callback (file read)'); +}); + +setTimeout(() => { + console.log('Timeout callback') +}, 0) + console.log('Synchronous task executed'); ``` @@ -381,7 +391,7 @@ console.log('Synchronous task executed'); - 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. - Use `process.nextTick()` for tasks that should execute before any I/O events, often useful for deferring operations or handling errors synchronously. -- Use `setImmediate()` for tasks that should run after I/O events but before timers. +– Use `setImmediate()` for tasks that should run after the current event loop’s I/O events, but before timers scheduled for the next loop iteration. 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')`). From dc189d67f134c5e29cb26406b3181da2158ec4d2 Mon Sep 17 00:00:00 2001 From: khappa Date: Wed, 23 Apr 2025 22:10:07 +0400 Subject: [PATCH 2/4] fix: make setImmediate docs more beginner-friendly. --- .../discover-promises-in-nodejs.md | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/apps/site/pages/en/learn/asynchronous-work/discover-promises-in-nodejs.md b/apps/site/pages/en/learn/asynchronous-work/discover-promises-in-nodejs.md index ea94be600ee30..1f0115a46d4ce 100644 --- a/apps/site/pages/en/learn/asynchronous-work/discover-promises-in-nodejs.md +++ b/apps/site/pages/en/learn/asynchronous-work/discover-promises-in-nodejs.md @@ -367,23 +367,13 @@ console.log('Synchronous task executed'); ### `setImmediate()` -`setImmediate()` schedules its callback for the event loop’s check phase—i.e. it will run immediately after all of the current loop’s I/O callbacks (poll phase) have completed. +`setImmediate()` is used to execute a callback immediately after all of the current loop's I/O callbacks have completed. ```js -const fs = require('fs'); - setImmediate(() => { console.log('Immediate callback'); }); -fs.readFile('./some.txt', () => { - console.log('I/O callback (file read)'); -}); - -setTimeout(() => { - console.log('Timeout callback') -}, 0) - console.log('Synchronous task executed'); ``` @@ -391,7 +381,7 @@ console.log('Synchronous task executed'); - 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. - Use `process.nextTick()` for tasks that should execute before any I/O events, often useful for deferring operations or handling errors synchronously. -– Use `setImmediate()` for tasks that should run after the current event loop’s I/O events, but before timers scheduled for the next loop iteration. +– Use `setImmediate()` for tasks that should run after the current event loop's I/O events. 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')`). From 758bf4daccc10e5f518a2169b25b690d4b255ab2 Mon Sep 17 00:00:00 2001 From: pakobarbakadze Date: Wed, 23 Apr 2025 22:32:50 +0400 Subject: [PATCH 3/4] fix: format --- .../en/learn/asynchronous-work/discover-promises-in-nodejs.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/site/pages/en/learn/asynchronous-work/discover-promises-in-nodejs.md b/apps/site/pages/en/learn/asynchronous-work/discover-promises-in-nodejs.md index 1f0115a46d4ce..7e710c747ce3d 100644 --- a/apps/site/pages/en/learn/asynchronous-work/discover-promises-in-nodejs.md +++ b/apps/site/pages/en/learn/asynchronous-work/discover-promises-in-nodejs.md @@ -381,7 +381,7 @@ console.log('Synchronous task executed'); - 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. - Use `process.nextTick()` for tasks that should execute before any I/O events, often useful for deferring operations or handling errors synchronously. -– Use `setImmediate()` for tasks that should run after the current event loop's I/O events. + – Use `setImmediate()` for tasks that should run after the current event loop's I/O events. 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')`). From 0b8d8c893bf5da23ba7f3c3b21ce8f6046649966 Mon Sep 17 00:00:00 2001 From: pakobarbakadze Date: Wed, 23 Apr 2025 22:47:12 +0400 Subject: [PATCH 4/4] fix: format --- .../en/learn/asynchronous-work/discover-promises-in-nodejs.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/site/pages/en/learn/asynchronous-work/discover-promises-in-nodejs.md b/apps/site/pages/en/learn/asynchronous-work/discover-promises-in-nodejs.md index 7e710c747ce3d..9484f21d30541 100644 --- a/apps/site/pages/en/learn/asynchronous-work/discover-promises-in-nodejs.md +++ b/apps/site/pages/en/learn/asynchronous-work/discover-promises-in-nodejs.md @@ -381,7 +381,7 @@ console.log('Synchronous task executed'); - 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. - Use `process.nextTick()` for tasks that should execute before any I/O events, often useful for deferring operations or handling errors synchronously. - – Use `setImmediate()` for tasks that should run after the current event loop's I/O events. +- Use `setImmediate()` for tasks that should run after the current event loop's I/O events. 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')`).