Techletter #21 | May 9, 2023
The event loop is the mechanism by which Node.js manages asynchronous operations
. In Node.js, most I/O operations are performed asynchronously using ***callbacks***
, so that the Node.js runtime can continue to execute other code while waiting for I/O operations to complete. When an asynchronous operation is initiated, a callback function is registered with the Node.js event loop, which will be called when the operation is complete.
What is an asynchronous operation?
An asynchronous operation is an operation that does not block the execution of the main thread
of a program, allowing other code to continue executing while the operation is being performed in the background.
Asynchronous operations are typically used in situations where the operation may take a long time to complete, or where the program needs to continue executing while waiting for the operation to complete.
The difference between synchronous and asynchronous code
A Synchronous code
is executed in a blocking manner, meaning that each line of code is executed in sequence, and the program waits for each line to complete before moving on to the next one.
const fs = require('fs');
const fileData = fs.readFileSync('./fileName.text', 'utf-8');
console.log(fileData);
An asynchronous code
is executed in a non-blocking manner, meaning that the program can continue to execute other code while waiting for an asynchronous operation to complete.
const fs = require('fs');
fs.readFile('./fileName.txt', 'utf-8', (err, fileData) => {
if (err) throw err;
console.log(fileData);
});
In the asynchronous code, you can see that we are making use of a callback. So what really is a callback? Let’s understand
What is a callback?
A callback is a function that is passed as an argument to another function and is called when the operation that the function performs is complete.
In the above asynchronous code, the callback function checks for any errors that may have occurred during the file read operation, and if there are no errors, it logs the contents of the file to the console.
The Node.js event loop runs continuously, checking for new events and executing the associated callback functions when they become available. It consists of two main parts: Event Queue & Event Loop.
There are 6 phases
in a nodejs event loop
Timers, I/O callbacks, idle-prepare(only used internally), Poll, check, close callback
- Timers: In this phase, the event loop checks for any timer events that have expired and executes their associated callback functions. Timer events can be created using functions such as
setTimeout()
orsetInterval()
. - I/O callbacks: The event loop executes any I/O-related callback functions that are ready to be executed. These can include callbacks for completed network requests, file I/O operations, or other I/O-related events.
- Idle, prepare: These phases are currently unused in Node.js.
- Poll: The event loop checks for new I/O events that are ready to be processed, such as incoming network requests or completed file I/O operations. If there are no I/O events to process, the event loop may block until a new event arrives.
- Check: In this phase, the event loop executes any callbacks that have been scheduled using
setImmediate()
. - Close callbacks: In this phase, the event loop executes any close-related callback functions, such as those associated with closing a network socket or file descriptor.
Refer event loop explanation in the official doc
Some of the helpful articles: