async function - await not waiting for promise

前端 未结 4 1215
生来不讨喜
生来不讨喜 2020-12-30 22:52

I\'m trying to learn async-await. In this code -

const myFun = () => {
    let state = false;

    setTimeout(() => {state = true}, 2000);

    return         


        
4条回答
  •  滥情空心
    2020-12-30 23:40

    If you're using async/await, all your calls have to use Promises or async/await. You can't just magically get an async result from a sync call.

    Your final call needs to be:

    getResult().then(response => console.log(response));
    

    Or something like:

    (async () => console.log(await getResult()))()
    

提交回复
热议问题