I am attempting to get a public IP address from an API and then having that IP to be used inside another function (ninjaUniqueVisitorRequest()
).
I have the following code:
function ninjaGetIp() {
var ipRequest = new XMLHttpRequest();
ipRequest.open('GET', "https://api.ipify.org?format=jsonp=", true);
ipRequest.send();
ipRequest.onload = function () {
if (ipRequest.status >= 200 && ipRequest.status < 400) { // If response is all good...
return ipRequest.responseText;
} else {
console.log('There was an error retrieving the public IP.');
return '127.0.0.1';
}
}
}
async function ninjaUniqueVisitorRequest() {
// var ninjaSiteUuid = ninjaGetSiteUuid();
// var ninjaFingerprint = await ninjaGetFingerprint();
var ninjaPublicIp = await ninjaGetIp();
console.log(ninjaPublicIp);
}
Currently, when I run ninjaUniqueVisitorRequest();
the console.log(ninjaPublicIp);
returns undefined
.
I kind of understand that it returns straight away before the request has been made however thats what I thought I was fixing by using async/await
.
Any ideas are appreciated! PHP guy over here, go easy on me.