0

I want to call functions of parse.com in for loop, but when I put functions into for loop then for loop gets completed without response from functions.

for (var i = 0; i <tourPackage.length; i++) { 
        var Apackage = tourPackage[i].attributes;  
        tourPackageDest.set("package_id",tourPackage[i].id);
       tourPackageDest.set("itinerary",Apackage.holiday_short_detail);
        var SD = Apackage.destination.split(',');
            for(var k=0; k<SD.length;k++){
                var destPlace =                           Parse.Object.extend("Cities_World");//DestinationMaster
                 var query = new Parse.Query(destPlace);
                query.equalTo("place_name", SD[k]);
                query.find().then(function(results){
                    console.log(results);
                });
                tourPackageDest.save(null,{
                    success: function(Tpackage){
                        console.log("Successfully saved in PackageItineraryMaster");
                        }
                });
            }

}

1 Answer 1

3

Many of those methods you're calling (e.g., .find(), .save()) are asynchronous. From the looks of it, you're writing the code as if they as synchronous methods. Here is an example (via the Parse docs) of how to structure your code to account for this asynchronicity:

var query = new Parse.Query("Comments");
query.equalTo("post", 123);

query.find().then(function(results) {
  // Collect one promise for each delete into an array.
  var promises = [];
  _.each(results, function(result) {
    // Start this delete immediately and add its promise to the list.
    promises.push(result.destroy());
  });
  // Return a new promise that is resolved when all of the deletes are finished.
  return Parse.Promise.when(promises);

}).then(function() {
  // Every comment was deleted.
});

Basically, you want to add each of the promises from the async functions to an array and create a new promise that resolves when all those promises are resolved.

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

1 Comment

Thank you Thank you thank you, I am just newbie in parse.com so taking time to resolve problems. But you make it more simpler for me. Thanks Again

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.