Chlebta
Chlebta

Reputation: 3110

Parse server each query wait until current job is done

This my code :

    Parse.Cloud.job("deleteDuplicatedMates", function(request, status) {

  var friendshipQuery = new Parse.Query("Friendship");
  friendshipQuery.each((friendship) => {

    var innerQuery1 = new Parse.Query("Friendship");
    innerQuery1.equalTo("user1", friendship.get("user1"));
    innerQuery1.equalTo("user2", friendship.get("user2"));

    var innerQuery2 = new Parse.Query("Friendship");
    innerQuery2.equalTo("user1", friendship.get("user2"));
    innerQuery2.equalTo("user2", friendship.get("user1"));

    var findPS = Parse.Query.or(innerQuery1, innerQuery2)
    .notEqualTo("objectId", friendship.id)
    .find()
    .then(function(objects) {
      console.log("did found");
      if (objects.length > 0) {
        //delete deplicated objects
        Parse.Object.destroyAll(objects);
      }
    })
    return Parse.Promise.when(findPS);
  }).then(function() {
      status.success("I just finished");
    }, function(error) {
      status.error("There was an error");
    })
  });

My code works fine but I need to update it so that : The next each(friendship) is treated only if the current one has finished deleting the found object, so the flow will be like this :

get first object => find duplicated objects => delete found objects => get the second object => find duplicated objects => delete them => get the this one ...

Upvotes: 0

Views: 144

Answers (2)

Vilém Jeniš
Vilém Jeniš

Reputation: 141

Assuming

Parse.Object.destroyAll(objects);

is asynchronous, simply returning the destruction promise should do the trick.

return Parse.Object.destroyAll(objects);

It's also hard to help you for real, given how little context you've given.

Upvotes: 1

Jake T.
Jake T.

Reputation: 4378

If a promise block reaches the end of the block, and hasn't returned another promise, it automatically returns an empty resolved promise. So, I would change .find() to return findPs.find().../*rest of code there*/

Upvotes: 1

Related Questions