I always struggle with understanding how async / await works.
So, I decided to write the code down and refer to this rather than trying to think it though every time!
I’ll start with the simple XMLHttpRequest
var req = new XMLHttpRequest();
req.onreadystatechange = function () {
req.onload = function () {
if (req.status == 200) {
console.info('success')
var arr = JSON.parse(req.responseText);
populatePage(arr);
}
else {
console.info('error')
}
}
}
req.open('GET', baseURL, true);
req.send();
Here, I’m hitting baseURL
(defined outside of this code) and doing something with the received array with the populatePage
function outside as well.
I wrote it like this so that I don’t add other code which is not relevant to the call.
Continue reading