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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:38:49+00:00 2026-05-17T23:38:49+00:00

I have the following piece of javascript but for some scoping reason the names

  • 0

I have the following piece of javascript but for some scoping reason the “names” that is returned from myfunc is empty.

var myfunc = function(client, id) {
  var names = new Array();
  client.query(
    'SELECT stuff FROM mytable WHERE id="'+id+'"',
    (function selectCb(err, results, fields) {
      if (err) {
        throw err;
      }
      for (result in results) {
        // This prints fine
        console.log(results[result].name);
        names[result] = results[result].name;
      }
      client.end();
    })
  );
  // The following returns empty
  return names;
}

console.log(myfunc(1,2));

How can I make it break out of scope?

  • 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-17T23:38:50+00:00Added an answer on May 17, 2026 at 11:38 pm

    It’s empty because the call to your “query” function is asynchronous. The function you pass into it won’t be run until the results are available. Therefore, your “myfunc” function returns immediately, long before that callback function is invoked.

    Using Javascript in a browser, you have to think in those terms. Instead of expecting your “names” to be ready immediately, change “myfunc” so that you pass it a callback function to be invoked when the names are actually available:

    var myfunc = function(client, id, whenFinished) {
      var names = new Array();
      client.query(
        'SELECT stuff FROM mytable WHERE id="'+id+'"',
        (function selectCb(err, results, fields) {
          if (err) {
            throw err;
          }
          for (result in results) {
            // This prints fine
            console.log(results[result].name);
            names[result] = results[result].name;
          }
          client.end();
          if (whenFinished) whenFinished(names); // callback
         })
      );
    };
    

    Now when you call your function, instead of expecting the “names” as a return value, you’ll pass in another function that will act on the list of names:

    myfunc(1, 2, function(names) { console.log(names); });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following piece of code that takes in some words, stores them
Have the following piece of javascript over on http://jsfiddle.net/mhenrixon/zPLgS/16/ and its working fine in
The template wont save when I have this piece of javascript in it: var
Possible Duplicate: Formatting a date in JavaScript I have the following piece of script.
Take the following piece of javascript that is trying to implement a class: function
Consider the following piece of code: <html><head></head> <body> <script type=text/javascript> var outside_scope = outside
I have the following piece of code ( Prototype ): $('table_cell_id') That cell contains
i have the following piece of code in the page HEAD: <script type=text/javascript> $(document).ready(function(){
I have written the following piece of code in my view page <script type=text/javascript>
I have following piece of code: It compiles without problems under gcc-3.4, gcc-4.3, intel

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.