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

  • Home
  • SEARCH
  • 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 8446261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:52:59+00:00 2026-06-10T09:52:59+00:00

I came across the need to return a value from one function to another

  • 0

I came across the need to return a value from one function to another and for a while scratched my head in confusion when this simple task didn’t work. I then realised I was operating within a closure function (or anonymous function?) and can’t find documentation on how to release a variable out of such a function’s scope.

For example, this doesn’t work:

function aFunc()
{
   var result;
   object.event = function(){
      result = true;
   }
   return result;
}

Nor does returning from inside the closure. Do I need to do both? I tried using a global variable within the largest scope possible (outside of all functions) and this didn’t work either. What am I missing?

I’m not sure whether I’m using the term closure correctly, I’m referring to the anonymous function.

Thanks.

  • 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-06-10T09:53:01+00:00Added an answer on June 10, 2026 at 9:53 am

    You need to call the closure:

    function aFunc() {
       var result;
       object.event = function() {
          result = true;
       };
       object.event();
       return result;
    }
    

    Or, if it runs elsewhere, this is a timing issue. You can have something like a promise:

    function aFunc() {
       var promise = { hasRun: false, result: null };
       object.event = function() {
          result.hasRun = true;
          result.result = true; // or something else...
       };
       return promise;
    }
    
    // check if it has run and get the result:
    
    if (promise.hasRun) {
      // access promise.result
    }
    

    But, a simple callback should suffice. Since you mentioned the XMLHttpRequest object, you should be able to attach a callback to its onreadystatechange event, or pass a callback to aFunc:

    function aFunc(callback) {
       object.event = function() {
          var result = true;
          callback(result);
       };
    }
    

    Then your callback gets called with the result at the time it is available.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using this IP Validation Function that I came across while browsing, it
I came across this (really) simple program a while ago. It just outputs the
I came across a piece of code which looks like this: jQuery(function($) { $('#saySomething').click(function()
I am reading C++ Templates Complete Guide and came across this non-type function template
I came across a function in sqlite that I need to convert to postgres
Today I came across the need to write a CAML query that uses WHERE
In a small project of mine I've came across the need of a collapsible
I need scheduling functionality on my .NET MVC website and I came across Quartz.net
Came across this code: <?php require_once 'HTTP/Session/Container/DB.php'; $s = new HTTP_Session_Container_DB('mysql://user:password@localhost/db'); ini_get('session.auto_start') or session_start();
Came across something like this today, and was wondering if there was an equivalent

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.