title: Cloud function continuously looping until I/O failure
body: <p>I'm trying to make a cloud function that uploads images to Dropbox and then creates and uploads a summary text file:</p>
  
  <pre><code>Parse.Cloud.define("transferToStore",function(request, response) {
  Parse.Cloud.useMasterKey();
  
  var token;
  var tokenQuery = new Parse.Query("StoreTokens");
  tokenQuery.equalTo("store", request.params.store);
  tokenQuery.find().then(function(results) {
      token = results[0].get("token");
  }).then(function() {
      var photoQuery = new Parse.Query("Uploads");
      photoQuery.equalTo("orderNumber", request.params.orderNumber);
      photoQuery.equalTo("store", request.params.store);
      photoQuery.find({
          success: function(results) {
              for (var i = 0; i &lt; results.length; ++i) {
                  var photo = results[i].get("photo");
                  var photoBuffer;
                  Parse.Cloud.httpRequest({
                      url: photo.url()
                  }).then(function(bufferResponse) {
                      photoBuffer = bufferResponse.buffer;
                      console.log('buffer has been generated: ');
                      var dropboxParams = new Object();
                      dropboxParams.path = '/' + request.params.orderNumber + '/' + results[i].get("size") + '/' + results[i].get("fileName");
                      dropboxParams.mode = 'add';
                      dropboxParams.autorename = true;
                      dropboxParams.mute = false;
  
                      console.log('accessing buffer: ');
  
                       // This part stopped working
                      Parse.Cloud.httpRequest({
                          method: 'POST',
                          url: 'https://content.dropboxapi.com/2/files/upload',
                          headers: {
                              'Authorization': 'Bearer ' + token,
                              'Dropbox-API-Arg': JSON.stringify(dropboxParams),
                              'Content-Type': 'application/octet-stream'
                          },
                          body: photoBuffer
                      }).then(function(httpResponse) {
                          console.log('upload - response:  status: ' + httpResponse.status + 'text: ' + httpResponse.text);
                      }, function(httpResponse) {
                          console.log('upload failed: status: ' + httpResponse.status + 'error: ' + httpResponse.text);
                          response.error('status: ' + httpResponse.status + 'error: ' + httpResponse.text + ' ');
                      });
                  }, function(bufferResponse) {
                      console.log('buffer response failed');
                  });
              }
          },
          error: function() {
              response.error('uploads query failed');
          }
      });
  
      //this part works and is within a loop until function returns i/o failure
      var Order = Parse.Object.extend("Orders");
      var order = new Order();
      order.set("store", request.params.store);
      order.set("orderNumber", request.params.orderNumber);
      order.set("name", request.params.name);
      order.set("phone", request.params.phone);
      order.set("email", request.params.email);
      order.set("type", request.params.type);
      order.set("payment", request.params.payment);
      var str = "number: " + request.params.orderNumber + "\r\n"
                  + "ordered by: " + request.params.name + "\r\n"
                  + "phone: " + request.params.phone + "\r\n"
                  + "email: " + request.params.email + "\r\n";
      var buff = new Buffer(str, 'utf8').toString("base64");
      var file = new Parse.File("order.txt", { base64: buff }, 'text/plain');
      order.set("summary", file);
      order.save().then(function() {
          Parse.Cloud.httpRequest({
              method: 'POST',
              url: 'https://api.dropboxapi.com/2/files/save_url',
              headers: {
                  'Authorization': 'Bearer ' + token,
                  'Content-Type': 'application/json; charset=utf-8'
              },
              body: {
                  'path': '/' + request.params.orderNumber + '/' + 'order.txt',
                  'url': file.url()
              }
          });
          respone.success('ok');
      }, function() {
          console.log('summary save failed');
          response.error('summary save failed');
      });
  });
  </code></pre>
  
  <p>});
  I put 2 comments in the code to try to explain the problem
  at the beginning the summary file worked fine and only 1 time and Dropbox upload worked, but the files were empty, because the part of code was after the file buffer, but because the file buffer wasn't generated it uploaded empty files.
  so I moved the upload httprequest to be inside the file buffer httprequest, and then the problem started.
  I also tried it with promises and had the same problem.</p>
  
  <p>I guess the problem is something about lack of response.success, but can't understand where.</p>