I am creating a pure JavaScript module and I'm trying to call some data from an API, this completes successfully and if I assign the response to a variable and then log it to the console then I see the expected results. However if I return it then I receive undefined. Apologies if this has been answered already but I was unable to find the answer to my question after browsing for some time.
My code is as follows:
httpRequest.onload = function() {
if(httpRequest.readyState === 4) {
if (httpRequest.status === 200) {
response = JSON.parse(httpRequest.responseText);
console.log(response) // Returns data correctly
return response;
} else {
console.log("API call error");
}
}
}
The above code returns to another function outside the module as follows:
console.log(module.callAPI(query)); //returns undefined
Any insignt would be appreciated.
Thanks!