js async/await not working

后端 未结 2 548
南旧
南旧 2021-01-27 05:44

I\'m trying to wrap my head around async/await and how to use them. I\'m following some examples I\'ve seen to the letter (I think), but it the await is not actually waiting for

2条回答
  •  失恋的感觉
    2021-01-27 06:10

    So here:

    $(document).ready(function () {
        let json = doAjaxGet( "/static/imeiXref.json");
        console.log('json: ', json);
        processJSONData( json );
    });
    

    You're not telling let json to wait for the response from doAjaxGet. Its awaiting inside the doAjaxGet function but everything else is executing sequentially without waiting.

    What you want to do is this (I think):

    $(document).ready(async function () {
        let json = await doAjaxGet( "/static/imeiXref.json");
        console.log('json: ', json);
        processJSONData( json );
    });
    

    Async/await is basically syntactic sugar around promises. await waits for a promise to resolve, so you have to do async/await at every level you're using the promise chain, or you can just use standard promise.then() style coding.

提交回复
热议问题