Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

147
Views
What microtasks are enqueued with a Promise that resolves with a Promise in JavaScript?

I am trying to understand what microtask get enqueued when a Promise is resolved with another Promise.

For example, what microtasks are enqueued with the following code?

const p1 = Promise.resolve("A");
const p2 = new Promise(resolve => resolve(p1));
p2.then(() => console.log("THEN"));

I have found no way to inspect the MicrotaskQueue in V8. After "reading" the ECMA specification of "Promise Resolve Functions", this is what I understood:

The call resolve(p2) in the executor of Promise p2 enqueues a microtask that chains p1 to p2. Lets call it the chaining microtask.

After the block of code is executed, the stack is empty and V8 processes the microtask queue. The queue contains only the chaining microtask.

The chaining microtask calls the then method of p1 passing as callback functions the resolve and reject functions of p2. p1 is a resolved promise therefore the call to then enqueues a new microtask to execute the then callbacks. Lets call it p1-then-p2 microtask.

The microtask queue now contains only the p1-then-p2 task.

V8 executes the p1-then-p2 task. The task enqueues a new microtask to execute the last then in the code: p2.then(() => console.log("THEN")). Lets call it p2-then-console.

The microtask queue now contains only the p2-then-console task. V8 executes the task printing "THEN" in console.

The microtask queue is empty, the program exits.

Do I understood correctly?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Do I understood correctly?

Yes.

However, don't write code that will depend on the order of microtasks. This is specified in detail for deterministic results across js implementations, not for user code to rely on. It might even change with ECMAScript versions.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!