Questions about the execution order of async await promise in use

    var aaa=function(data){
        bbb().then(()=>{
            console.log(4)
        })
        console.log(3)
    }

    var bbb= async function (){
        console.log(1)
        await ccc();
        console.log(2)
    }

    var ccc=function(){
        console.log(5)
    }
    
    aaa() // 1 5 3 2 4

the output is 1 5 3 2 4
Why not 1 5 2 3 4?

Jan.08,2022

the overall process is like this. The entire execution process of aaa is divided into three Synchronize code segments.
1: enter aaa- > enter bbb- > log (1)-> enter ccc- > log (5)-> exit to aaa- when await,bbb is suspended > register then callback-> log (3)-> exit after aaa execution
2: event cycle call up bbb to continue execution-> log (2)-> bbb exit after execution
3: event cycle call up then callback-> log (4)-> exit

so it is 15324, and the key point is that after the async method executes to await, it will actively suspend waiting for scheduling.

The question of

is nothing more than why output 32, not 23
because output 3 is Synchronize code, do not need to wait for the fullfilled status of any asynchronous operation. The
output 2 is asynchronous and cannot be run until the end of the await ccc () execution, that is, until the ccc () switches to the fullfilled state before continuing.
so first 3 and then 2

Menu