Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

102
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!