DEV Community

Cover image for Event loop in node js
Grace Valerie Anyango
Grace Valerie Anyango

Posted on

Event loop in node js

What is an event loop?

JavaScript code runs in the sequence it is defined since it is synchronous.
The event loop is what allows node js to perform non-blocking I/O operations(operations that allow a single process to perform multiple requests at the same time) despite the fact that JavaScript is single-threaded.

How it works

The node js environment is made up of input/output terminals, the Call stack, node APIs and the Callback queue. if your program is synchronous you will only need to focus on the call stack.

With the non-blocking behavior, all the synchronous events are executed first while the asynchronous functions are processed in the background while waiting to be executed.

Suppose we have a program

console.log('Hello, I am first')
setTimeout(()=>{
 console.log('Finally, I get executed')
}, 0)
console.log('Let me wait for asynchronous function')

Enter fullscreen mode Exit fullscreen mode

The output is as follows

Hello, I am first
Let me wait for asynchronous function
Finally, I get executed
Enter fullscreen mode Exit fullscreen mode

When the program starts executing, an anonymous main() function is usually added to the call stack which wraps up the code. The first and last console.log are pushed to the call stack and executed in the output terminal since they are synchronous. The setTimeout function is asynchronous and is therefore not processed in the call stack but is added to the Node API where its events are registered and a callback function is waiting to be processed in the background.

Phases of the event loop

Phases of the event loop diagram

  1. Timers
    These are functions that execute callbacks after a set period of time. This module has two global functions, setInterval() and setTimeout() where you can define code that will execute after a certain time period.

  2. I/O Callbacks
    This phase allow for non-blocking I/O operations. Asynchronous I/O requests are recorded in the queue so that the main call stack can continue its operations.
    code example

const {readFile} = require('fs')
console.log('first task has started')
// checking the file path and returning context of the text file
readFile('./myFile/text.txt', 'utf8',(err,result) => {
if (err) {
  console.log(err)
  return 
}
console.log(result)
console.log('First task has been completed')
})
console.log('starting my second task)
Enter fullscreen mode Exit fullscreen mode

The output is as follows

first task has started
starting my second task
I am content inside the text file
First task has been completed
Enter fullscreen mode Exit fullscreen mode
  1. Idle
    This is the phase where the event loop performs internal operations of any callbacks.

  2. Poll phase
    All JavaScript code written is executed from the beginning to end. The event loop manages the I/O workload, calling the functions in the queue until the queue is empty.

  3. setImmediate()
    This is a special timer in node js which runs as soon as the poll phase becomes idle.

  4. Close events
    Is used to clean the state of the application when the event loop is wrapping up one cycle and is ready to move on to the next one.

Top comments (0)