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

314
Visualizações
Why we have to use keyword async and await to making it synchronus in node, when the keyword is literally async?

So I was thinking that ok node is asynchronous (meaning multiple lines of code execute at the same time), then why we use the keyword async( which literally means asynchronous) to wait for a task to be done by keyword await (that is making it synchronous). Why there is not a keyword sync, because that's what we are doing? Can someone please explain the logic of keyword async? I am so confused.

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

0

The async keyword enables the use of await which is just a more streamlined syntax for writing code with promises and asynchronous operations. It does not make anything synchronous. It just allows you to use similar programming techniques as synchronous code, but it is all still asynchronous.

Can someone please explain the logic of keyword async? I am so confused.

When you use the async keyword to declare a function, you are creating a different type of function that ALWAYS returns a promise. This is useful only for asynchronous programming - thus the async keywords's affiliation with the term "asynchronous".

Further, the async keyword does not make anything synchronous. It does allow you to use a more synchronous-like programming model which is a lot simpler to use, particularly for things like conditional branches in asynchronous control flow, but nothing that was asynchronous actually becomes synchronous.

As an example of how nothing becomes synchronous, run this example in the snippet and note the order of console.log() statements. The async function returns when it hits the first await and the rest of the execution continues which the asynchronous operation is still going. This is a simulation with a timer, but that timer could be any other asynchronous operation such as an http request, a database operation, etc...

function delay(t) {
    return new Promise(resolve => {
        setTimeout(resolve, t);
    });
}

async function run() {
   console.log("in run() 1");
   await delay(100);
   console.log("in run() 2");
}

console.log("before run()");
run();
console.log("after run()");

You can see from running this little example, that the run() function has its execution suspends as soon as you hit the await and it returns allowing the other code after the function call to run. Then, sometime later when the timer fires and the delay() promise is resolved, the run() function picks up where it was suspended and executes the rest of the function body.

This is operationally the same as this code using .then() instead of async and await:

function delay(t) {
    return new Promise(resolve => {
        setTimeout(resolve, t);
    });
}

function run() {
   console.log("in run() 1");
   delay(100).then(() => {
       console.log("in run() 2");
   });
}

console.log("before run()");
run();
console.log("after run()");

about 4 years ago · Juan Pablo Isaza Relatório

0

Node.js is not "asynchronous," but instead, Node.js has a set of asynchronous APIs.

To answer your question about async/await - async/await is just syntactic sugar as it uses promises under the hood.

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