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
Promise doesn't seem to be resolved with await

I have found something interesting while playing with Promises and awaits:

function returnPromise() {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve('result of promise');
    }, 1000);
  });
}


async function sth() {
  let result = await returnPromise();
  console.log('result: ', result);
  console.log('flag 1');
}

sth();

In this first example, the result of the promise prints out correctly. However, consider the second example:

function returnPromise() {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve('result of promise');
    }, 1000);
  });
}

async function sth2() {
  let result = returnPromise();
  await result;
  console.log('result: ', result);
  console.log('flag 1');
}

sth2();

Here, the result is {} (which I think means the Promise is still pending). This doesn't make sense to me because I thought the line 'await result' was supposed to block till the Promise resolved.

My question is:

  1. Is this expected behavior? If it is, what is the logic behind it?
  2. If this isn't what's expected, why is this happening?

Thank you.

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

0

I thought the line 'await result' was supposed to block till the Promise resolved.

Well, it blocks further execution of that async function - but yes.

But the result is still a Promise.

await will take the thenable on its right side, wait for it to finish, and extract the resolve value from it. But the thenable will remain a thenable - it doesn't change the Promise itself (it doesn't change the Promise expression into the value it resolves to).

await someThenable

extracts the resolve value from someThenable, but someThenable remains a thenable - logging someThenable, whether before or after the await, will still log the thenable, and not the resolve value.

about 4 years ago · Juan Pablo Isaza Denunciar

0

let result = returnPromise();

This line sets the value of result to be a reference to the Promise object. It does not automagically change to be the result of the awaited promise whenever you call await on it later on.

You can tell by the fact that there's still a waiting period that the await keyword is working. However, you're throwing away the result value.

await will give you and rvalue that you can assign to a variable:

let actualResult = await result;
// or
result = await result; // but you shouldn't do this
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