简体   繁体   中英

Make the function call synchronous

function first(){
    console.log("1")
}

function second(){
     new Promise ((resolve,reject)=>{
        setTimeout(function(){
            console.log("2")
            resolve();
        } ,0);

     })  
}

function third(){
    console.log("3")
}

 async function run(){
   
    first();
    await second();
    third();

}

run();

Need to make the function call sync to get final output as 1,2,3 i tried creating the promise and use async await but that didnt help any other way

Pack the setTimeout into a promise and resolve in setTimeout,

Use async await for that promise in order for that to run consecutively

 function first() { console.log("1") } function second() { return new Promise(res => { setTimeout(function() { console.log("2"); res() }, 0) }) } function third() { console.log("3") } async function run() { first(); await second() third(); } run();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM