Share:- Whatsapp Facebook Facebook

------------------------------------- Welcome to Developerhelpway Q&A, where you can ask questions and receive answers from other members of the community.

What is event loop node.js

0 votes
14 views
What is event loop node.js ?
asked Nov 11, 2018 in Node.js by Ashish Asawa

1 Answer

0 votes
As we know that Node.js is a single-threaded application, but it supports concurrency via events and callbacks. An API of Node.js is asynchronous and single-threaded, they use async function calls to maintain concurrency.

Node.js follows the observer pattern. Node.js thread keeps an event loop and whenever a task gets completed, it fires the corresponding events which signals the event-listener function to execute.

Function of Node.js Event Loop:-

When Node starts its server, it simply initiates its variables, declares functions and then simply waits for the event to occur.

An event-driven application generally have a main loop that listens for events, and then triggers a callback function when one of those events is detected.


 ┌───────────────────────────┐
┌─>│           timers          │
│  └─────────────┬─────────────┘
│  ┌─────────────┴─────────────┐
│  │     pending callbacks     │
│  └─────────────┬─────────────┘
│  ┌─────────────┴─────────────┐
│  │       idle, prepare       │
│  └─────────────┬─────────────┘      ┌───────────────┐
│  ┌─────────────┴─────────────┐      │   incoming:   │
│  │           poll            │<─────┤  connections, │
│  └─────────────┬─────────────┘      │   data, etc.  │
│  ┌─────────────┴─────────────┐      └───────────────┘
│  │           check           │
│  └─────────────┬─────────────┘
│  ┌─────────────┴─────────────┐
└──┤      close callbacks      │
   └───────────────────────────┘
answered Nov 21, 2018 by john12 (3,120 points)
...