1

I'd like to run two queries to the db - or whatever parse.com uses - consecutively.

So i have:

var query = Parse.Query("table1");
....

var query2 = Parse.Query("table2");
...

The first query is just checking some stuff to make sure that everything is ok with the request - that the device version is right etc. The second query is the actual point of the whole exercise.

Because the first query is being asked for pretty much all the functions i'm using, I'd like to keep it separate. I don't really want to keep defining it for each function, and then sub-calling the second query - that's repetitive and stupid.

Is there some elegant way - ie not whiling on a boolean and setting the boolean to true in the first queries' success method - to run the two queries consecutively?

1 Answer 1

2

I broke the queries up into two functions,

function1(){
    var query = Parse.Query("a");
    ...
}

function2(){
    var query = Parse.Query("b");
    ...
}

Then from within the success of function1() I call function2 - or rather, I call a given parameter (assuming it isn't undefined!)

function function1(onSuccess){
    var query = Parse.Query("a");
    query.first("c");
    query.find(){
        success : function(){
            doCoolThings();
            if (onSuccess != null){
                 onSuccess(); 
            }
        },
        error: function (){ } 
    }
}

And now I have a reusable component that I can use as I see fit!

BONUS: what if function2 requires paramters?

So! In the main function that calls both these two, we have:

Parse.Cloud.define("myCoolFunction",function(request,response){ 
    var onSuccessCall = function(){ function2(request, response); };
    function1(request,response,onSuccessCall);
});

function function2(request, response){ ... }

with function1 defined as above.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.