简体   繁体   中英

typescript Await is not working in callback

I have two functions and I am calling cbf() from func() through callback and I am using await but after callback comes first.

function cbf(name, callback: Function) {
    console.log(name)
    callback("123")
}

function async func() {
    await cbf("alice", function(aa) {
        console.log(aa)
    })
    console.log("after callback")  
}

You should use Promise

 function cbf(x) { return new Promise(resolve => { setTimeout(() => { resolve(x); }, 2000); }); } async function f1() { var x = await cbf(10); console.log(x); // 10 } f1(); 

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