Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1109579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:15:31+00:00 2026-05-17T02:15:31+00:00

How does one return from a closure, without returning from the containing function? In

  • 0

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();
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T02:15:32+00:00Added an answer on May 17, 2026 at 2:15 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Beginner rails question: How does one return a file from a controller in rails?
The thing is that it does return one row. Here's the thing. SELECT... FROM...
I have the following Java Class that does one thing, fires out values from
How does one write a (Intel) F90 function that converts a string into lowercase
how does one go about accessing a decorator from a base class in a
This one DOES NOT work fine: ALTER PROCEDURE rconte.spPesquisaPesIdDadoCodigo24 ( @pPesCodigo24 char ) AS
My question is when does any one need to return an object by reference?
I need to return data from two tables - one table contains records on
Where does one access the data from a parsed xml file when using libxml?
What does one do with this kind of error? You are reading lines from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.