Q:

Azure Javascript - Wait for Function Completion

I'm using Azure Mobile Services and am running a javascript backend. However, since the backend is in node.js, everything is executed asynchronously and I don't know how to halt the execution of a function. 
I'm trying to delete a club if there hasn't been a comment in it within the past 24 hours, and here's my code:
var clubs = tables.getTable('Club');
clubs.read(
    {
        success: function(club){
            var now = new Date().getTime();
            for(var i=0;i<club.length;i++){
                var deleteClub = true;
                comments.where({ClubId: club[i].id}).read(
                    {
                        success:function(comment){
                            var timeDiff = (now-comment[i].Time.getTime())/(1000*60*60);
                            console.log("Comment in table: "+timeDiff);
                            if(timeDiff<24){
                                deleteClub=false;
                            }

                        }
                    }
                );
                if(deleteClub){
                    console.log("deleting club: "+club[i].Title);
                    //clubs.del(club[i]);
                }else{
                    console.log("saving club: "+club[i].Title);
                }
            }
        }
    }
);

The if statement executes before the delete club variable is updated, so it's always true, but I need the if statement's execution to be delayed until after all of the comments have been looped through.

A:

Since the callback you get is asynchronous, you can't use any information you're getting in that callback in synchronous code after the where call.
Since we want to handle things on a club-by-club basis, first we'll move the handling of clubs into its own function. This avoids the problem that by the time we get our callback from read, i will have been incremented.
Your code seems to assume success is called repeatedly, once for each comment. I don't think that's likely to be the case, more likely it's called once, with a list/array of the matching comments.
If so, then splitting off club handling to its own function and then looping the found comments should do it:
var clubs = tables.getTable('Club');
clubs.read(
    {
        success: function(allClubs){ // <== Note plural
            var now = new Date().getTime();
            for (var i = 0; i < allClubs.length; i++) {
                handler(now, allClubs[i]); // <== Move handling off to a handler
            }
        }
    }
);
function handler(now, club) {   // <== Unlike `i` above, `club` won't change because it's
                                // a function argument that we never assign to
    comments.where({ClubId: club.id}).read(
        {
            success: function(comments){ // <== Note the plural
                var deleteClub = true;
                for (var i = 0; i < comments.length; ++i) {
                    var timeDiff = (now-comments[index].Time.getTime())/(1000*60*60);
                    console.log("Comment in table: "+timeDiff);
                    if(timeDiff<24){
                        deleteClub=false;
                    }
                }
                if (deleteClub){
                    console.log("deleting club: "+club.Title);
                    //clubs.del(club);
                }else{
                    console.log("saving club: "+club.Title);
                }
            }
        }
    );
}

