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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:27:47+00:00 2026-06-15T09:27:47+00:00

I am having a small issue with what I believe is probably my misunderstanding

  • 0

I am having a small issue with what I believe is probably my misunderstanding of Javascript closures.

I have this piece of code —

getStdOpts: function(tbl, filt) {
    var vals = new Array();

    this.srvs.getStdOptions(
        { tbl: tbl },
        {
            'ok': function(rsp) {
                for (var i in rsp) {
                    vals.push({ value: rsp[i].id, text: rsp[i].descr });
                }
            }
        }
    );
    return vals;
}

In essence, although the inner function inside the getStdOptions call (‘ok’: function…) pushes new values into the vals array, when accessed from outside the call, the vals array is empty. When accessed from within the inner function, vals contains all the elements as expected.

Would really appreciate any help I can get on this matter.

  • 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-15T09:27:48+00:00Added an answer on June 15, 2026 at 9:27 am

    I doubt this is a closure/scope issue. If this.srvs.getStdOptions is an asynchronous operation, your getStdOpts is always going to return an empty array. This array would be filled once the operation completes, which, as written, would be after you would need it. You’re going to have to handle things a bit differently. Either you need to directly pass into getStdOpts a callback which would take vals as a parameter and execute that callback within your anonymous one for this.srvs.getStdOptions, or you need to return some sort of promise object to which you can add callbacks (which would essentially take the same vals as a parameter) as needed– you’d have to resolve in your anonymous callback your promise to have vals as its “promised” results.

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

Sidebar

Related Questions

ok I am having this small issue with CSS. I have a menu div
I'm having a small issue, I have some code that calculates a mean value
Ok I am having a small issue I am trying to turn this jQuery
Im having a small problem with my code. I have been trying to read
I'm having a small and weird issue using Hibernate. I have two tables: SERVICE
I have inherited a piece of software which is having some issues. I believe
I'm having a small issue with a bit of jQuery: function getQuickResults(terms) { var
I'm having a small issue with my annotate(sum()) function. Now what I want it
im having a problem with a small issue within this query $update = mysql_query(UPDATE
I am having a small issue with the mysqli_stmt prepare function. Here is my

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.