【问题标题】:async.js parallel not working correctlyasync.js 并行无法正常工作
【发布时间】:2014-01-07 03:53:12
【问题描述】:

我有以下代码:

               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++;      
                });

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

【问题讨论】:

    标签: node.js async.js


    【解决方案1】:

    异步回调的第一个参数应该是错误对象,所以如果一切正常,它应该真的返回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 中有一个示例,它准确地显示了它是如何完成的。

    【讨论】:

      猜你喜欢
      • 2021-12-22
      • 2013-06-12
      • 1970-01-01
      • 2016-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多