JavaScript Event Loop

Md Solayman
1 min readMay 5, 2021

--

During the code execution if there is any asynchronous function in the stack, javascript doesn’t execute that function instantly rather it assigns the task to WEB API (In case of Browser), C++ API (Node Js) and do the execution of next task in the call stack. When the WEB API completes the task, it transfers the result to an another data structure called Call Back Queue (follows the First In First Out (FIFO) data structure). If there are many asynchronous functions, they all are transferred to call back queue after the task is completed in the web api. If there is any promises in the WEB api then WEB api will transfer its ( promises) call back to an another queue that is called MICRO TASK QUEUE (ES6 feature) which has more priority to the event loop than the callback queue. Then when the synchronous tasks are done in the stack, by the “Event Loop” JavaScript engine takes all the results to call stack from the call back queue or micro task queue according to the preference by FIFO method and do execution.

For this reason javascript is called single threaded but event driven programming language that enables javascript to do multiple tasks at the same time.

--

--

Md Solayman

An enthusiastic learner , currently focused on Web Development using HTML, CSS, Bootstrap, JavaScript , React, Node, Express Js, Mogodb.