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

101
Visualizações
Async/Await function execution sequence

I state that I am not an expert in JavaScript, and I am now starting to deepen it.

This post aims to clarify some doubts about the async / away functions that I'm just starting to deepen.

The example below leaves me perplexed on the sequence of execution that seems, from the logs printed on the console, different from what I expected.

Before exposing my doubt and my question for clarification I show you my example code:

async function fetchAlbum(id){
    let album;
    album = await fetch(albumUrl + '/' +id)
    .then( resp => resp.json())
    .then(albumcontent => albumcontent);

    console.log('Concrete Value 1',album); //Here is printed the concrete value thanks to await is not undefined
    
    return album; //Here the concrete value is wrapped by a promise (the value is returned to the caller in the form of a promise)
}

let alb = fetchAlbum(1);
console.log('Promise 1',alb); //Print a promise... will be the first logged (!?)
alb.then(alb => console.log('Concrete Value 2',alb)); //We have the album again as a concrete value (the album object)

I created an async function fetchAlbum within which I resolve the promises returned by the fetch () API by prefixing await. Therefore, I expect that at the call fetchAlbum (1) the code waits until the "return album" is performed inside the function async fetchAlbum (id). But in reality, this is not the case. Looking at the tracks on the console, I see:

"Promise 1", then "Concrete Value 1" and finally "Concrete Value 2", the latter printed by the callback function of the promise returned by the async fetchAlbum function.

What I understand is that JavaScript (ES6), just called an async function, returns the promise continuing with the flow of execution following the call let alb = fetchAlbum (1); and as soon as the chain of promises is finished, it will print "Concrete Value 1" and finally, it will pass for "return album".

The question is, assuming my guess is correct, how does JavaScript immediately return the promise (in pending state) without going through the "return"?

I am still a bit confused and any suggestion to understand will be very welcome for me.

I hope I have exposed my perplexity and my question in the most understandable way possible.

Thanks a lot.

about 4 years ago · Juan Pablo Isaza
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