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

103
Vistas
output order involve promise, micro task queue in js

new Promise((resolve, reject) => {
  console.log("promise1")
  resolve()
}).then(() => {
  console.log("then11")
  new Promise((resolve, reject) => {
    console.log("promise2")
    resolve()
  }).then(() => {
    console.log("then21")
  }).then(() => {
    console.log("then23")
  })
}).then(() => {
  console.log("then12")
})

The above code output is following, When I test my understanding of micro task and macro task with promise and I can not get the correct output.

// "promise1"
// "then11"
// "promise2"
// "then21"
// "then12"
// "then23"

To be more specific, I am confused about the last 3 outputs. Thanks for the help!

// "then21"
// "then12"
// "then23"
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

If you're looking for this output:

// "promise1"
// "then11"
// "promise2"
// "then21"
// "then23"
// "then12"

Then return the Promise inside your first .then(, like this:

new Promise((resolve, reject) => {
  console.log("promise1")
  resolve()
}).then(() => {
  console.log("then11")
  return new Promise((resolve, reject) => {
    console.log("promise2")
    resolve()
  }).then(() => {
    console.log("then21")
  }).then(() => {
    console.log("then23")
  })
}).then(() => {
  console.log("then12")
})

The return returns the Promise made by the last .then( in the chain after your second new Promise. Your very last .then( will await the then23 .then(, then execute.

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