Search code examples
javascriptasync-awaites6-promise

Await question in Javascript async functions


async abc(){
await some().then(() => {
   //do inside then
});

//other code 
}

Does the "await" wait only on the some() or does it await the some() and its then() before going to //other code? Basically the question is does the await wait also on the then part to be done before moving to next statement.


Solution

  • some().then() returns a new promise and the await waits on that new promise so it will wait for the .then() handler and any promise that it might return before going on past the await. In other words, it waits for that entire promise chain.


    In general, this is not great style to mix await and .then() in the same statement as you would typically want to stick with await instead of .then() as in:

    async abc(){
        let result = await some();
        // do something with result
    
        //other code 
    }
    

    This gives you the simpler looking and more sequential code design that await enables.