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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:37:14+00:00 2026-06-17T09:37:14+00:00

So in my server code, variable invites is undefined outside of the success function.

  • 0

So in my server code, variable invites is undefined outside of the success function.

function getInvites(id){
    var InvitesTable = tables.getTable("Invites").where({"PlanID": id}).select("UserID","Attending");
    var invites;
    InvitesTable.read({ success: function(resultss) { 
                           invites = resultss;
                           console.log(invites); //works here
                           }});
    console.log(invites); //undefined here
}

From similar questions, I realize its because of it being asynchronous. So the success function call is run after the console.log(invites); //undefined here call.

My question is how do I stop that in Windows Azure?

Added code

function read(query, user, request) {

        request.execute({
            success: function(results) {
                results.forEach(function(r) {

                    getInvites(r.id, function(invites) {
                        r.invites = invites;
                    });
                });
                request.respond();
            }
        });

}

function getInvites(id, cb){
    var InvitesTable = tables.getTable("Invites").where({"PlanID": id}).select("UserID","Attending");
    InvitesTable.read({ success: function(results) {
                           if (cb) cb(results);
                           }});
}
  • 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-17T09:37:14+00:00Added an answer on June 17, 2026 at 9:37 am

    You don’t “stop that,” you design your application around the async nature of whatever environment you’re using.

    I assume you’re trying to do something like this:

    function getInvites(id){
        var InvitesTable = tables.getTable("Invites").where({"PlanID": id}).select("UserID","Attending");
        var invites;
        InvitesTable.read({ success: function(resultss) { 
                               invites = resultss;
                               }});
        return invites;
    }
    
    // later...
    
    var invites = getInvites(someId);
    //do something with `invites`
    

    This obviously won’t work, since you return the value of invites before the async call completes.

    Instead, you write your app in async style:

    function getInvites(id, cb){
        var InvitesTable = tables.getTable("Invites").where({"PlanID": id}).select("UserID","Attending");
        InvitesTable.read({ success: function(resultss) { 
                               if (cb) cb(resultss);
                               }});
    }
    
    // later...
    
    getInvites(someId, function(invites) {
        //do something with `invites`
    });
    

    This leaves out error handling code for the sake of simplicity, so you’d have to add that as well.


    After seeing your full code, it looks like you have a simple problem of managing many parallel asynchronous operations. Consider what happens: your loop runs, iterating over an array of n objects. For each, you call getInvites, which begins a database request and returns.

    This means your loop runs very quickly, but now you have n outstanding database requests that you must wait on before you can call request.respond().

    An extremely basic solution would be to do something like count the number of times your getInvites callback is called, and then finally complete the request when that number reaches n.

    However, it is time-consuming and mistake-prone to manage this bookkeeping manually every time you make async requests. This is a situation where flow control libraries are extremely useful. I will use jQuery’s Deferred in this example, since it may already be familiar to you (even if you don’t know you’ve actually used it before — if you’ve ever used jQuery’s XHR API, you’ve used Deferreds).

    Given that you’re in a server environment, you obviously don’t have jQuery; however, there are people who have extracted only the code necessary for Deferred for you.

    Once we have Deferreds for every pending request, we can use when to register a callback that gets called only after all pending Deferreds complete.

    function read(query, user, request) {
    
            request.execute({
                success: function(results) {
                    var dfds = [];
                    for (var i = 0; i < results.length; i++) {
                        dfds.push(getInvites(results[i].id)); // Makes an array of Deferreds representing
                                                              // each of our pending requests.
                    }
    
    
                    Deferred.when.apply(Deferred, dfds) // see details below
                        .done(function() {
                            for (var i = 0; i < results.length; i++) {
                                results[i].invites = arguments[i]; // Copy each set of invites to each result
                            }
                            request.respond(); // We're done!
                        })
                        .fail(function() {
                            // Handle errors here
                        });
                }
            });
    
    }
    
    function getInvites(id){
        var dfd = new Deferred(); // Create a new Deferred, which automatically starts in the 'pending' state
        var InvitesTable = tables.getTable("Invites").where({"PlanID": id}).select("UserID","Attending");
        InvitesTable.read({ success: function(results) {
                               dfd.resolve(results); // When we get data back, we 'resolve' the Deferred --
                                                     // in other words, say its operation is done,
                                                     // and pass along the operation's results.
                               },
                            error: function(err) { // TODO: Not sure if this is how the API you're using handles errors
                                dfd.reject(err); // Marks the Deferred as failed.
                            }});
    
        return dfd.promise(); // We (synchronously) return the Promise.  The caller can attach event handlers
                              // to the Promise, which are invoked when we eventually resolve or reject the Deferred.
    }
    

    Notes:

    • jQuery.when (or in this server-side case, Deferred.when) normally expects you to pass a fixed number of Deferreds as arguments:

      $.when(dfd1, dfd2).done(function(result1, result2) { ... });
      

      However, we have a variable number of Deferreds, so we must apply an array of Deferreds to when and then in the done handler, access each result via the implicit arguments object.

    • Array.forEach(...) is slow. In most cases, it is better to use a regular for loop.

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

Sidebar

Related Questions

I have this jQuery code: <script type=text/javascript > $(function() { $('#add_comment').bind('submit',function() //$(input[type=submit]).click(function() { var
This is my server code I have a problem because my program freeze and
Here is the server code package echoserver; import java.net.*; import java.io.*; public class EchoServer
I have this code for server code in c, under unix: /* A simple
I have declared an enum in my server code, and would like my client
Let's say you had a fairly basic client/server code, where each client creates three
I'm trying to do some server/socket programming. The server code is written in plain
So just recently, my async server code which relies on pthread_mutex_timedlock seem to have
here is the server side code of my program, the problem is, its accepting
I have my server side code create an event on Facebook, using a Facebook

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.