Skip to content

Latest commit

 

History

History
43 lines (31 loc) · 2.16 KB

68-Event Loop.md

File metadata and controls

43 lines (31 loc) · 2.16 KB

The Event Loop

In JavaScript, the event loop is a mechanism that handles asynchronous operations by continuously monitoring the call stack and the task queue. When an asynchronous operation completes, it is placed in the task queue. The event loop then checks the call stack, and if it's empty, it takes the next task from the queue and pushes it onto the call stack to be executed.

The event loop runs continuously, constantly checking the call stack and task queue for new tasks to execute. This allows JavaScript to handle multiple tasks at the same time without blocking the main thread.

For example, when you make an AJAX request or set up a timer with setTimeout(), those operations are sent to the browser's event loop to be processed asynchronously. While those operations are waiting for their completion, other code can continue to execute on the main thread.

console.log('start');

setTimeout(() => {
  console.log('setTimeout');
}, 0);

Promise.resolve().then(() => {
  console.log('Promise');
});

console.log('end');

In this example, we have three tasks: a setTimeout function, a Promise, and a series of console.log statements.

When we run this code, the following happens:

  1. The first console.log statement prints 'start' to the console.
  2. The setTimeout function is added to the task queue, with a delay of 0 milliseconds. Note that this does not mean that the function will run immediately; it will only be executed when the event loop gets to it.
  3. The Promise is resolved and the then callback is added to the microtask queue.
  4. The second console.log statement prints 'end' to the console.
  5. At this point, the call stack is empty, so the event loop checks the queues for pending tasks.
  6. It first checks the microtask queue, finds the then callback, and executes it, printing 'Promise' to the console. Next, it checks the task queue, finds the setTimeout function, and executes it, printing 'setTimeout' to the console. So the final output will be:
start
end
Promise
setTimeout

In summary, the event loop is a key concept in JavaScript's ability to handle asynchronous operations, allowing for non-blocking I/O and concurrent execution of code.