From 22677fe160fe5da824431815d516883d40a3b30c Mon Sep 17 00:00:00 2001 From: khappa Date: Wed, 23 Apr 2025 21:32:56 +0400 Subject: [PATCH 1/7] 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 55119614f2bec..ecfa678bab4e0 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 f7978124101c572ab257c6989e11c22f7351fdce Mon Sep 17 00:00:00 2001 From: khappa Date: Wed, 23 Apr 2025 22:10:07 +0400 Subject: [PATCH 2/7] 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 ecfa678bab4e0..703bfeb0464ab 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 1401408be9f4f759f6007d8495084edbf5967884 Mon Sep 17 00:00:00 2001 From: pakobarbakadze Date: Wed, 23 Apr 2025 22:32:50 +0400 Subject: [PATCH 3/7] 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 703bfeb0464ab..12320fb90f679 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 ed79579ed50f94f38e3c12b86be2700191d528c1 Mon Sep 17 00:00:00 2001 From: pakobarbakadze Date: Wed, 23 Apr 2025 22:47:12 +0400 Subject: [PATCH 4/7] 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 12320fb90f679..cc907a4fdcdef 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 2867757978ef7d6c4739e40eba9ba4bfdff4401b Mon Sep 17 00:00:00 2001 From: pakobarbakadze Date: Mon, 19 May 2025 16:30:49 +0400 Subject: [PATCH 5/7] fix: drop 'immediately' --- .../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 cc907a4fdcdef..0879b4006b719 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,7 +367,7 @@ console.log('Synchronous task executed'); ### `setImmediate()` -`setImmediate()` is used to execute a callback immediately after all of the current loop's I/O callbacks have completed. +`setImmediate()` is used to execute a callback after all of the current loop's I/O callbacks have completed. ```js setImmediate(() => { From 4c3d46d3cbc1ced6f01fd965fa8eeb06ec3b21a8 Mon Sep 17 00:00:00 2001 From: pakobarbakadze Date: Sun, 25 May 2025 18:22:36 +0400 Subject: [PATCH 6/7] fix: correct event loop terminology --- .../en/learn/asynchronous-work/discover-promises-in-nodejs.md | 4 ++-- 1 file changed, 2 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 0879b4006b719..ae71d6f8b628d 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,7 +367,7 @@ console.log('Synchronous task executed'); ### `setImmediate()` -`setImmediate()` is used to execute a callback after all of the current loop's I/O callbacks have completed. +`setImmediate()` schedules a callback to be executed in the check phase of the Node.js event loop, which runs after the poll phase, where most I/O callbacks are processed. ```js setImmediate(() => { @@ -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 poll phase, once most I/O callbacks have been processed. 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 cd1df94a7a725081baa4661fee38499999d286ef Mon Sep 17 00:00:00 2001 From: pakobarbakadze Date: Fri, 4 Jul 2025 09:57:04 +0400 Subject: [PATCH 7/7] docs: link event loop --- .../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 ae71d6f8b628d..dbb0065a6ba5e 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,7 +367,7 @@ console.log('Synchronous task executed'); ### `setImmediate()` -`setImmediate()` schedules a callback to be executed in the check phase of the Node.js event loop, which runs after the poll phase, where most I/O callbacks are processed. +`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. ```js setImmediate(() => {