node.js - async.js 并行无法正常工作

标签 node.js 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++;      
                });

这不起作用,我的控制台将回调中完成的第一个查询显示为结果。输出中也没有 {one: fullResults,two: gpsThresholds} 格式,它只是显示各个函数的回调值。

最佳答案

异步回调的第一个参数应该是错误对象,因此如果一切正常,它实际上应该返回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(null, fullResults);

等等,将 null 传递给异步回调的错误处理程序。
documentation中有一个例子准确显示它是如何完成的。

关于node.js - async.js 并行无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20957812/

相关文章:

node.js - 使用 async wait 执行多个数据库查询

javascript - Express4.js - 仅当有事件的 Passport.js session 时才路由

javascript - 在 async.auto 链中使用 async/await 会导致 TypeError : callback is not a function

javascript - ReactJS:获取JSON

javascript - 如何在 async.js waterfall 流中传递上下文或属性?

javascript - 为什么每个任务的参数计数在 async.waterfall 中应该相同

node.js setEncoding 问题

javascript - 从另一个包执行 npm 脚本

javascript - 如何在 producthunt 之类的 NodeJS 中定义快速路由以逐日获取数据?

javascript - 跳出 javascript 嵌套的 async.each 循环但继续主循环