I've got the following code which does an ajax (initFunction.initFunction(email)) to validate a user email. The ajax works, however the response always returns 'undefined'
const somefunction = (email) => {
let x = new Promise(function(resolve, reject) {
let y = initFunction.initFunction(email) //ajax call which returns true
resolve (y)
});
return x;
}
async function logFetch(email) {
try {
const response = await somefunction(email)
.catch(function(ex) {console.log('error')});
console.log(await response);
}
catch (err) {
console.log('fetch failed', err);
}
}
logFetch('foo@bar.com');
initFunction.initFunction():
const xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
xhr.open('GET', '/email/verify/'+email);
xhr.onreadystatechange = function() {
if (xhr.readyState>3 && xhr.status==200) {
if (xhr.responseText == "unavailable") {
return true
} else if (xhr.responseText == "invalid") {
return false
} else if (xhr.responseText == "valid") {
return true
}
}
};
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded', 'Access-Control-Allow-Origin');
xhr.send(null);
return xhr;