简体   繁体   中英

Wait for httpRequests to finish before sending response - ParseServer

I'm trying to understand how promises work but I'm not getting the results. I've tried with lots of examples, nothing works.

This code is a Parse cloudcode function that receive an array o string, each of them are part of a url that I request, so, if I receive an array of 3 elements, the code has to make the httprequest of 3 url's gather the 3 responses and send it to the client. This is what I've done:

Parse.Cloud.define('getInfo', function(request,response) {

  var placas = request.params.placa;

  x(placas, function(result){
    response.success(result);
  }, function(error){
    response.error(error);
  });
});

function x(placas,callback,error){
  var url1 = 'http://XXXXX/';
  var promises = [];

  for(var i=0; i<placas.length ;i++){
    var url2 = url1.concat(placas[i]);
    promises.push(requestMulta(url2));
  }

  var data = Parse.Promise.as(promises);
  callback(data);
}

function requestMulta(url){
   var promise = new Parse.Promise(); 
   Parse.Cloud.httpRequest({
      url: url ,
      headers: {
         'Content-Type': 'application/json;charset=utf-8'
      }
   }).then(function(httpResponse) {
       ...
       promise.resolve(data);
   }, function(httpResponse) {
    //Handle error
   });
   return promise;
}

The response I get is:

{
    "_rejected" = 0;
    "_rejectedCallbacks" =     (
    );
    "_resolved" = 1;
    "_resolvedCallbacks" =     (
    );
    "_result" =     (
            (
            "<null>", // 3 null's because I'm testing with an array o 3 elements
            "<null>",
            "<null>"
        )
    );
}

By checking the documentation I've following input:

Parse.Cloud.define('getInfo', function(request,response) {

  var placas = request.params.placa;

  x(placas).then(function(result){
    response.success(result);
  }).error(function(error){
    response.error(error);
  });
});

function x(placas,error){
  var url1 = 'http://XXXXX/';
  var promises = [];

  for(var i=0; i<placas.length ;i++){
    var url2 = url1.concat(placas[i]);
    promises.push(requestMulta(url2));
  }

 return Parse.Promise.when(promises);

}

You're using Parse.Promise.as wich is resolving the promise immidiently, check documentation for Parse.Promise.as .

Check the documentation for Parse.Promise.when .

I ended up changing things, I hope this is useful to someone.

Instead of using Promise, I installed the library Async ( http://caolan.github.io/async/ ) which helps you to make multiple async functions and it's pretty easy to use.

New Code:

Parse.Cloud.define('getInfo', function(request,response) {

  var placas = request.params.placa; //this is an array whith the variables I'll use in the functons

  x(placas).then(function(result){
    response.success(result);
  }).error(function(error){
    response.error(error);
  });
});

function x(placas,callback,error){

    async.map(placas, requestMulta, function(err, results) {
        console.log(results)
        callback(results);
    });
}

function requestMulta(placa, callback){

    var url = 'http://XXXXX/' + placa;
    Parse.Cloud.httpRequest({
        url: url ,
        headers: {
            'Content-Type': 'application/json;charset=utf-8'
        }
    }).then(function(httpResponse) {

        //... (The process you want)...

        callback(null,data);

    }, function(httpResponse) {

        var data = {};
        console.error('Request failed with response code ');
        error("Error");
    });

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM