I'm trying to work with a XMLHttpRequest and got the basic code working, but can't get the function with the code below to return a boolean variable to say if it's worked or not:
var xhr = new XMLHttpRequest();
var success = false;
xhr.open('HEAD', sourceurl, true);
xhr.send();
xhr.onreadystatechange=function(){
    if (xhr.readyState === 4){   //if complete
        if(xhr.status === 200){  //check if "OK" (200)
            //success
            console.log('success');
            return true;
        } else {
            // fail
            console.log('fail' );
            return false;
        }
    } 
};
The console logs show the right thing but when wrapping this code in a function it simply won't give any indication on whether it worked or not.
Any help would be appreciated!
EDIT: Not a duplicate as cannot use jQuery in my example and existing solutions there do not work.
 
    