title: Parse.Cloud.afterSave not working with parse-server
body: <p>I was using parse.com and had written a cloud code function that worked perfectly. When I moved to a self hosted parse-server backend, some of the cloud code function stopped working.</p>
  
  <pre><code>Parse.Cloud.afterSave("League", function (request) {
  
      if (request.object.get("leaderboard") == null) {
  
          var leaderboard = Parse.Object.extend("Leaderboard");
          var newInstance = new leaderboard();
  
          newInstance.save(null , {useMasterKey: true})
              .then(function (result) {
                  request.object.set("leaderboard", result);
                  request.object.save(null ,{useMasterKey: true});
              },
              function (error) {
                  console.log("Error");
              });
          });
      }else{
          var membersRelation = request.object.relation("members");
          var membersQuery = membersRelation.query();
          membersQuery.count(null , {useMasterKey: true})
              .then(function (totalNumber) {
                  request.object.set("memberCount", totalNumber)
                  request.object.save(null ,{useMasterKey: true});
              }, function (error) {
                  console.log("Error")
              })
       }
  </code></pre>
  
  <p>As you can see, I define the <code>afterSave</code> hook for the <code>League</code> class. In my hook, I have to update the same object again when I set a new value (leaderboard and/or membersCount) so save is called save more than once. </p>
  
  <p>The function saves data properly, but it causes an infinite loop too. I understand it happens because I call <code>request.object.save()</code> that will change the <code>League</code> class again so the <code>afterSave</code> event fires again, and so on. I don't know how I can handle this condition. Someone suggested to me to add a timeout but not sure how.  Can you please help with a solution to this problem.</p>
  
  <p>Thanks</p>