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
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.