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

203
Vistas
Why do these two await lines execute in parallel?

In below code, both c and c2 variables are awaiting for different promise instances and should run in serial but when run, the program completes in 100 miliseconds instead of 200.

function a1() {
  return new Promise((x, y) => {
    setTimeout(function () {
      x(5);
    }, 100);
  });
}

async function run() {
  let t1 = Date.now();
  let b = a1();
  let b2 = a1();

  let c = await b;
  let c2 = await b2;
  console.log(Date.now() - t1);
}

run().then(function () {
  console.log('@');
});

Why is this behavior legal if await is defined as a serial operation?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You are calling a1() for both b and b2 when declaring them. Thus, by the time you await, the timeouts have already run and they each hold a promise, the await just resolves those promises.

If you want to see the full delay you should asign a1 to your b variables and then await the calls when declaring c, or simply await a1() twice.

  let b = a1;
  let b2 = a1;

  let c = await b();
  let c2 = await b2();

function a1() {
  return new Promise((x, y) => {
    setTimeout(function () {
      x(5);
    }, 100);
  });
}

async function run() {
  let t1 = Date.now();

  let b = await a1();
  let b2 = await a1();
  
  console.log(Date.now() - t1);
}

run().then(function () {
  console.log('@');
});

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