简体   繁体   中英

How to merge the all the callback results in final callback using Async parallel

I have this scenario where I have to multiple CLI commands, which returns JSON , read each result and take the specific fields and finally merge them into a final JSON . All the CLI commands are independent each other.

var merge = require('./object-assign');

async.parallel(
  [
    function(callback) {
      var response = {},
          error {};
      var test = exec(command);

      test.stdout.on('data', function(data) {
        response = data;
      });

      test.stderr.on('data', function(data) {
        error.message = data;
      });

      test.on('close', function() {
        //callback1
        callback(error, response);
      })
    }, 

    function(callback) {
      var response = {},
          error {};
      var test = exec(command);
      test.stdout.on('data', function(data) {
        response = data;
      });

      test.stderr.on('data', function(data) {
        error.message = data;
      });

      test.on('close', function() {
        //callback2
        callback(error, response);
      })
    }
    //Few more callbacks 

  ], function(err, results) {
    //using object-assign to merge
    var test = merge(result[0], result[1]);
    //when callback1 completes result[0] is getting values where as result[1]
    //is undefined since it is getting executed.
  }
);

callback1 completes first and sends response to final callback with result, and callback2 completes and sends its response. How make sure all callbacks are done in the final callback so that I can merge the result to get the final JSON ?

Is async parallel the right approach here? If not, what is the best for this case?

Take a read at the Reference for async.js parallel

Run the tasks array of functions in parallel, without waiting until the previous function has completed. If any of the functions pass an error to its callback , the main callback is immediately called with the value of the error. Once the tasks have completed, the results are passed to the final callback as an array .

Note : parallel is about kicking-off I/O tasks in parallel, not about parallel execution of code. If your tasks do not use any timers or perform any I/O, they will actually be executed in series. Any synchronous setup sections for each task will happen one after the other. JavaScript remains single-threaded.

Example:

async.parallel([
    function(callback){
        setTimeout(function(){
            callback(null, 'one');
        }, 200);
    },
    function(callback){
        setTimeout(function(){
            callback(null, 'two');
        }, 100);
    }
],
function(err, results){
    // the results array will equal ['one','two'] even though
    // the second function had a shorter timeout.

    //manipulate the responses
    //JSON.stringify(results);
});

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