Return from closing?

How to return from closure without returning from contained function?

In the following function, the statement returnactually returns from GM_xmlhttpRequest: not closing. Naturally, I can see that I could arrange my code so that this execution shifts from the end of the close, but I'm curious how to return to the previous 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();
};
+3
source share
1 answer

return ( ) ( "parent" ), . , onreadystatechange, ( ).

GM_xmlhttpRequest undefined xhr.send() onreadystatechange, , XHR . "zomg wtf" , .

ECMA-262, 3- 5- ( 12.9 ):

return . Expression , undefined. .

+2

Source: https://habr.com/ru/post/1766997/


All Articles