How does one return from a closure, without returning from the containing function?
In the following function, the return
statement actually returns from GM_xmlhttpRequest
: not the closure. Naturally I can see that I could arrange my code so that that execution drops off the end of the closure, but I'm curious as to how to early return in the example.
function GM_xmlhttpRequest(details, callback) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState != 4)
return; // <-- zomg returns from parent function wtf
if (xhr.status != 200)
callback(null);
callback(xhr);
}
xhr.open('GET', details.url, true);
xhr.send();
};