开发者

Return from closure?

开发者 https://www.devze.com 2023-01-17 18:30 出处:网络
How does one return from a closure, without returning from the containing func开发者_高级运维tion?

How does one return from a closure, without returning from the containing func开发者_高级运维tion?

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();
};


return will only ever exit the callee (current function) and return control to the caller (calling "parent" function), it will never the return from the caller. In the situation you describe, the callee is the anonymous function set to onreadystatechange and there is no caller (per se).

GM_xmlhttpRequest returns undefined after the xhr.send() line before the onreadystatechange function runs because there is no return statement and the XHR is asynchronous. The "zomg wtf" line will just exit that anonymous function since there is no caller to pass control back to.

From ECMA-262, 3rd and 5th editions (section 12.9 The return statement):

A return statement causes a function to cease execution and return a value to the caller. If Expression is omitted, the return value is undefined. Otherwise, the return value is the value of Expression.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号