Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

148
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda