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 8150301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:01:49+00:00 2026-06-06T15:01:49+00:00

I looked at this: Returning values from nested functions in Javascript but it did

  • 0

I looked at this: Returning values from nested functions in Javascript

but it did not really help me (or I am just too dumb to get it).

My variable scope is somehow off and I don’t understand why. My alert() does not behave as expected. Tried to add comments on all lines to explain what I’m thinking.

Thanks very much for any comments/pointers/answers!

var g = {}; / is a large object with all kinds of other stuff and functions in it

g.ding = function(){ // my problem function
 var baby = 'young'; // i thought I set a local var here
    if(someVar==true) { // standard issue if statement
        someAPI.class( // using an API that uses a function as its attribute
            function(stuff){ // my anonymous function
                baby = 'old'; // setting the var to something
            }
        );
    }
    return baby; // returning the var
}

alert( g.ding() ); // i expect it to say "old" but it keeps saying "young". why?

NEW EDIT:
Juan’s accepted answer is great, but isn’t there also a way to use setTimeout() to deal with asynchronous function calls and essentially making them synchronous? if anyone who reads this knows the answer I’d love to know. 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-06T15:01:50+00:00Added an answer on June 6, 2026 at 3:01 pm

    The call to someAPI.class(function(){}) is probably asynchronous, that means the function you pass into it may not have been called when someAPI.class() returns and the variable hasn’t been changed.

    The solution is to pass the variable back in a callback

    g.ding = function(callback){ 
        var baby = 'young';
        if(someVar) {
            someAPI.class(
                // There's no guarantee of when this function is called
                function(stuff){
                    baby = 'old';
                    // Call the callback, it's like a return, for async functions
                    callback(baby);
                }
            );
        }
        // the inner function probably wasn't called yet, 
        // doesn't make sense to return this here
        // return baby;
    }
    
    // Then you'd call it like this, as soon as you use an async function
    // all the functions that call it have to use callbacks for return values
    g.ding(function(value){
        alert(value);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have looked at this page for some time now. Amazing, really. But I
This looked like a very simple question but I couldn't find the answer. How
I looked for this but i found how to use it with ScrollTo. This
I've looked at this question: Select first instance only with XPath? But if I
I just looked at this posting: What is the best or most interesting use
Ok I have looked into this, and I'm not sure if anyone else has
I am a bit lost here as I have not really looked at model
I have a while loop running that is returning values from a MYSQL table.
MySQL noob here; looked around first but couldn't find the answer to this question.
This was something I looked at a while back but couldn't figure it out.

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.