async.js并行工作不正常
我有以下代码:
async.parallel({ one: function(callback) { gps_helper.get_gps(PEMSID, conn, start, stop, function(fullResults){ callback(fullResults); }) }, //query for new dataSet two: function(callback) { admin.getTh(function(gpsThresholds){ callback(gpsThresholds); }) } }, function(results){ console.log(results); console.log('emitting GPS...'); socket.emit('GPS', {gpsResults: results.one, thresholds: results.two, PEMSID: PEMSID, count: count, length: PEMSToDisplay.length, checked: checked}); count++; });
这是行不通的,我的控制台显示第一个查询在callback中结束。 在输出中也没有{one: fullResults, two: gpsThresholds}
格式,它只是显示各个函数的callback值。
asynchronouscallback的第一个参数应该是错误对象,所以如果一切正常,它应该真的返回null
function(err, results){ console.log(results); console.log('emitting GPS...'); socket.emit('GPS', {gpsResults: results.one, thresholds: results.two, PEMSID: PEMSID, count: count, length: PEMSToDisplay.length, checked: checked}); count++; });
callback也一样
callback(null, fullResults);
等等,将null
传递给error handling程序的asynchronouscallback。
文档中有一个例子显示了它是如何完成的。