I have just started to learn Promise
s in Javascript, and happened upon async/await
.
In my understanding, if I specify a function, to be async
, javascript will always wrap the content of my function and return a Promise
instead of whatever I am returning.
Let's presume the following scenario:
async function httpRequest() {
oReq = new XMLHttpRequest();
oReq.addEventListener("load", function() {
return this.responseText;
});
oReq.open("GET", "http://some.url.here");
oReq.send();
}
httpRequest().then(function(httpResult) {
console.log(httpResult);
});
Am I correct in assuming, that the above situation will not work, as httpRequest()
is not returning anything, the callback of XMLHttpRequest
returns something only, thus my result will most likely be undefined
?
If yes, how would I be able to fix httpRequest()
so it will work in an async/await
scenario?