Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Timers, nextTick, and setImmediate | Understanding the Event Loop and Asynchronous Foundations
Node.js Event Loop and Asynchronous Code

bookTimers, nextTick, and setImmediate

index.js

index.js

copy

The event loop in Node.js manages the execution of asynchronous operations by dividing them into phases. The functions setTimeout, setImmediate, and process.nextTick are all scheduled differently within these phases.

First, the synchronous code runs, so Start and End are printed immediately. Then, process.nextTick callbacks are executed right after the current operation completes, before the event loop continues to the next phase. This is why nextTick appears next, even before timers or immediates.

Next, the timers phase of the event loop executes callbacks scheduled by setTimeout. Even though the timeout is set to 0, it is not executed immediately but waits for the timers phase. After that, the setImmediate callback runs during the check phase of the event loop, which comes after the timers phase.

This output demonstrates that process.nextTick callbacks are always executed before any other asynchronous operations, regardless of their scheduling. setTimeout and setImmediate can sometimes appear in different orders depending on the environment, but in this example, setTimeout comes before setImmediate because both were scheduled from the main module context.

Understanding this order is crucial when writing asynchronous Node.js code, as it helps you predict and control the flow of your program.

Note
Note

When setTimeout(..., 0) and setImmediate() are called from inside an I/O callback (for example, after a file read), setImmediate executes first because it's queued in the check phase, which runs right after I/O callbacks.

question mark

Which callback is executed first after the synchronous code, and why?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 3

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Awesome!

Completion rate improved to 7.69

bookTimers, nextTick, and setImmediate

Свайпніть щоб показати меню

index.js

index.js

copy

The event loop in Node.js manages the execution of asynchronous operations by dividing them into phases. The functions setTimeout, setImmediate, and process.nextTick are all scheduled differently within these phases.

First, the synchronous code runs, so Start and End are printed immediately. Then, process.nextTick callbacks are executed right after the current operation completes, before the event loop continues to the next phase. This is why nextTick appears next, even before timers or immediates.

Next, the timers phase of the event loop executes callbacks scheduled by setTimeout. Even though the timeout is set to 0, it is not executed immediately but waits for the timers phase. After that, the setImmediate callback runs during the check phase of the event loop, which comes after the timers phase.

This output demonstrates that process.nextTick callbacks are always executed before any other asynchronous operations, regardless of their scheduling. setTimeout and setImmediate can sometimes appear in different orders depending on the environment, but in this example, setTimeout comes before setImmediate because both were scheduled from the main module context.

Understanding this order is crucial when writing asynchronous Node.js code, as it helps you predict and control the flow of your program.

Note
Note

When setTimeout(..., 0) and setImmediate() are called from inside an I/O callback (for example, after a file read), setImmediate executes first because it's queued in the check phase, which runs right after I/O callbacks.

question mark

Which callback is executed first after the synchronous code, and why?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 3
some-alt