title: Running synchronous queries in Parse Framework Cloud Code
body: <p>I've written an asynchronous cloud background job for the Parse Framework that generates a display username for each user based off of their email before the '@' symbol. Unfortunately I'm getting an error "too many count operations" when I run the job. Is there a way to have the queries and saves run in series as opposed to in parallel? I saw in the documentation it is possible with promises, but I'm confused as to how to make it work with nested queries.</p>
  
  <pre><code>Parse.Cloud.job("generateUsernameForEveryUser", function(request, status) {
  
    // Set up to modify user data
    Parse.Cloud.useMasterKey();
    var counter = 0;
    // Query for all users
    var query = new Parse.Query(Parse.User);
    query.each(function(user) {
  
      createUsernameForUser(user, 0, {
        success: function(username) {
          if(username == null) {
            status.error();
          } else {
            user.set("displayUsername", username);
            user.set("displayUsernameUppercase", username.toUpperCase());
            user.save();
          }
        },
        error: function(error) {
          status.error("Error: " + error.message);
        }         
  
      });
  
    }).then(function() {
      // Set the job's success status
      status.success("Username generation completed successfully.");
    }, function(error) {
      // Set the job's error status
      status.error("Uh oh, something went wrong.");
    });
  
  });
  
  function createUsernameForUser(user, count, callback) {  
  
    var generatedUsername = user.getEmail().substring(0, user.getEmail().indexOf("@"));
  
    if(count &gt; 0) {
      //Quotes added to ensure no math is done between generatedUsername and count
      generatedUsername = "" +  generatedUsername + "" + count;
    }
  
    var userQuery = new Parse.Query(Parse.User);
    userQuery.equalTo("displayUsernameUppercase", generatedUsername.toUpperCase());
    userQuery.count({
  
      success: function(userCount) {
  
        if(userCount &gt; 0) {
          createUsernameForUser(user, count + 1, {
            success : function(responseUsername) {
              callback.success(responseUsername);
            }, 
            error: function(error) {
              callback.error(error.message);
            }
          });
        }
  
        else {
          callback.success(generatedUsername);
        }
  
      },
      error: function(error) {
        console.log("Error trying to count users: " + error.message);
        callback.error("Error trying to count users: " + error.message);
      }
  
     });//End of userQuery call
  
  }
  </code></pre>