Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Event Loop: Queue vs Message Queue vs Event Queue

Reading through a lot of JavaScript Event Loop tutorials, I see different terms to identify the queue stores messages ready to be fetched by the Event Loop when the Call Stack is empty:

  • Queue
  • Message Queue
  • Event Queue

I can't find the canonical term to identify this.

Even MDN seems to be confused on the Event Loop page as it calls it Queue first, then says Message Queue but in the tags I see Event Queue.

Is this part of the Loop being defined somewhere in details, or it's simply an implementation detail with no "fixed" name?

like image 512
Flavio Copes Avatar asked Apr 09 '18 09:04

Flavio Copes


People also ask

What is event loop and event queue in JavaScript?

A JavaScript runtime uses a message queue, which is a list of messages to be processed. Each message has an associated function that gets called to handle the message. At some point during the event loop, the runtime starts handling the messages on the queue, starting with the oldest one.

Is event loop a queue?

Event loop: An event loop is something that pulls stuff out of the queue and places it onto the function execution stack whenever the function stack becomes empty.

What is event loop and event queue in node JS?

Event loop is an endless loop, which waits for tasks, executes them and then sleeps until it receives more tasks. The event loop executes tasks from the event queue only when the call stack is empty i.e. there is no ongoing task. The event loop allows us to use callbacks and promises.

What is an event loop and callback queue and microtask queue?

The Callback queue is handled by the JavaScript engine after it has executed all tasks in the microtask queue. Microtask queue is processed after the current task is finished. The Callback queue is processed after the microtask queue is empty. Microtask queue is processed in a separate event loop.


1 Answers

Good question, I'm also a advocate of using proper terminology.

Queue, message queue, and event queue are referring to the same construct (event loop queue). This construct has the callbacks which are fired in the event loop.

Interestingly there are two different queues the job queue and the event loop queue. The job queue is specifically designed for promises. The job queue has a higher priority than the event loop queue, so if there are both callbacks available in both queues the ones in the job queue will be put on the stack first.

Hopefully this answers your question.

like image 139
Willem van der Veen Avatar answered Oct 07 '22 10:10

Willem van der Veen