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

183
Visualizações
Why does awaiting a void function call affect exiting the block?

According to MDN Web Docs, "await" is used to "wait for a Promise", but the expression following the keyword can also be "any value to wait for", hence not necessarily a Promise.

In the below demo the awaited value is a call to a void function, i.e. a function that implicitly returns "undefined". Note: the called function does not return a Promise. What exactly does it mean to await a void function call?

Demo: Add keyword "await" to the beginning of line 11, and see how it changes logging from "FC" to "CF".

"use strict";

function pr() {
  return Promise.resolve(null);
}

function invokeAsyncOperation(cb) {
  pr().finally(cb);
}
async function main() {
  try {
    // demo: await the following statement
    invokeAsyncOperation(() => console.log("C"));
  } catch (e) {} finally {
    console.log("F");
  }
}
main();

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You're seeing the result of two things:

  1. From the docs you linked:

    If the value of the expression following the await operator is not a Promise, it's converted to a resolved Promise.

    Loosely speaking,¹ await x where x is not a promise is the same as await Promise.resolve(x). So when you use await, there's always a promise involved.

  2. Promise completion handlers are always called asynchronously, x and await x have different timing: The former is synchronous; the latter is asynchronous:

    • A promise is created, fulfilled with x.
    • await puts a completion handler on the promise.
    • Since the promise is already fulfilled, a call to that completion handler is put in the microtask queue to be handled when microtasks are run at the end of the current task.
    • When microtasks are run, the promise completion is reached and does the console.log.

You see the log for C before the log for F because the completion handler logging C is already in the microtask queue before the completion handler for F is added, so it gets run first.


¹ Actually, Promise.resolve is always involved, even when x is already a promise. But the extra promise gets optimized away if x is already a native promise (of the same concrete class). You can see the resolve operation in Step 2 of the algorithm for await.

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