tonitone120
tonitone120

Reputation: 2310

Is their any relationship between the event-queue and the microtask queue?

My Understanding

It's been described to me that for asynchronous operations, once they've 'loaded', their return value enters the event-queue.

It's also been described to me that Promises & .thens are asynchronous operations and their return values stack up on the micro-task|PromiseJobs queue.

If we define 'front-of-the-queue' as the destination for the result of the first asynchronous operation 'loaded'...

I know the call-stack/macro-task queue has to be empty before the front of the event-queue or micro-task queue is loaded onto it.


My Question

Is there any relationship between the event-queue and the microtask queue?


Unrelated code-snippet

const f = () => {
  console.log('1')
  new Promise((resolve, reject) =>
    resolve(2)
  ).then(resolve => console.log(resolve))
}

f()

counter = 0;

for (let index = 0; index < 1000000000; index++) {
    counter = index;
}
console.log(3);

Upvotes: 0

Views: 80

Answers (1)

Shubham Dixit
Shubham Dixit

Reputation: 1

The microtask queue or job queue was introduced in ES6 with the introduction of promises.

There is no any such relationship but there is a difference ,the event queue puts the task at the back of the queue, behind all the other tasks ,while microtask queue puts it in the front and executes the task right after the current function in the callstack is executed.

Overall the priority of microtask queue > event queue (Valid for both Node js and Frontend Javascript).

More Explaination here

Upvotes: 1

Related Questions