// ================================================ var spawn = require('child_process').spawn; var async = require('async'); function spawnFunc(options, cb) { var child = spawn(options.cmd, options.args, function); var result = ''; child.stdout.on('data', function (data) { // do some stuff with stdout data result += data; }); child.stderr.on('data', function (data) { // do some stuff with stderr data }); child.on('close', function (code) { // error if (code !== 0) { cb('ps process exited with code ' + code); return; } // success cb(null, result); }); } function run() { var commands = [{ cmd: 'cmd1', args: ['args1'] }, { cmd: 'cmd2', args: ['args2'] }]; async.mapSeries(commands, spawnFunc, function (err, results) { if (err) { throw err; } // handler results }); } run(); // ================================================ Or you can use async.series, async.waterfall Another option is to use defer chain with bluebird or Q PS: If you only need to get the final stdout, and don't need to read every line of the stdout, exec might be a better option than spawn
【在 B****g 的大作中提到】 : // ================================================ : var spawn = require('child_process').spawn; : var async = require('async'); : function spawnFunc(options, cb) { : var child = spawn(options.cmd, options.args, function); : var result = ''; : child.stdout.on('data', function (data) { : // do some stuff with stdout data : result += data; : });