1

I'm trying to find a way to send multiple requests (using Express) and process all the response in one function.

Here's my code :

  // In router.js
  app.get('/api/FIRST_PATH', CALLBACK_FUNCTION_A );

 // In CALLBACK_FUNCTION_A file :
 module.exports = function (req, response) {
   CALLBACK_FUNCTION_TO_SERVICE_A();
   CALLBACK_FUNCTION_TO_SERVICE_B();
   CALLBACK_FUNCTION_TO_SERVICE_C();
}

My problem is to send the requests CALLBACK_FUNCTION_TO_SERVICE_A, CALLBACK_FUNCTION_TO_SERVICE_B and CALLBACK_FUNCTION_TO_SERVICE_C and then retrieve all the results in another function to process them.

Any help would be greatly appreciated.

1
  • 1
    use request-promise to do requests that return a promise, then do something when those promises are finished Commented Jun 2, 2016 at 10:17

2 Answers 2

3

You can learn more about the new js standard and use Promise.

// In CALLBACK_FUNCTION_A file :
module.exports = function (req, response) {
   var promises = [CALLBACK_FUNCTION_TO_SERVICE_A(), 
      CALLBACK_FUNCTION_TO_SERVICE_B(),
      CALLBACK_FUNCTION_TO_SERVICE_C()];

   Promise.all(promises).then( function(results) {
       //results is an array
       //results[0] contains the result of A, and so on
   });
}

Of course CALLBACK_FUNCTION_TO_SERVICE_A() and such need to return Promise objects. You form a function like this:

function asyncFunction(callback) {
   //...
   callback(result);
}

You can create a Promise like this:

var p = new Promise(asyncFunction);

It'll start running the function, and supports the Promise interface.

So for example, either use request-promise or you can do something like:

function CALLBACK_FUNCTION_TO_SERVICE_A() {
   var worker = function(callback) {
       app.get('/api/FIRST_PATH', callback);
   };

   return new Promise(worker);
}

You can read more about Promise and how to also easily handle errors.

Sign up to request clarification or add additional context in comments.

1 Comment

Mind Blowing ! Thanks a lot
1

You could use async parallel. You can keep all your API calls as async.parallel array or JSON(Example uses Array).

async.parallel(
 [
    function(done){
      reqServcieA(..., funnction(err, response){
        if(err) done(err,null);
        done(null, response);
      }
    },
    function(done){
      reqServcieA(..., funnction(err, response){
        if(err) done(err,null);
        done(null, response);
      }
    },
    ...// You can keep as many request inside the array

 ], function(err,results){
   // Will be called when all requests are returned
   //results is an array which will contain all responses as in request arry
    //results[0] will have response from requestA and so on
 });

1 Comment

I had tried using async parrallel. I guess I didn't use it right. Thank you for the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.