Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

100
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda