javascript - async.waterfall 仅返回函数数组的 array[0] 索引处的函数结果集 - Node js

标签 javascript node.js node-async

以下代码仅返回getpricingSummary的结果集

async.waterfall([
    function(callback){ 
            getpricingSummary(elementsParam, function(workloadinfo) {
            callback(workloadinfo);         
            });
    },
    function(callback){
        getPricingforResourceIdentifiers('vm/hpcloud/nova/small,image/hpcloud/nova/ami-00000075', function(pricingDetail) { 
            callback(pricingDetail);
        });
    }],
    function(result){
        console.log(result);
    });
]);

最佳答案

async 库遵循常见的 Node.js 模式:error-first 回调。

要表示任务“成功”,第一个参数应为falsy(通常为null),第二个参数为任意数据或在论证之后。

callback(null, workloadinfo);
callback(null, pricingDetail);
function (error, result) {
    if (error) {
        // handle the error...
    } else {
        console.log(result);
    }
}

另请注意 async.waterfall()旨在将结果从一个任务传递到下一个任务,仅得出最终任务的结果(或错误)。

如果您想收集每个任务的结果,请尝试 async.series() 。有了它,result 将是从每个任务传递的数据的Array

关于javascript - async.waterfall 仅返回函数数组的 array[0] 索引处的函数结果集 - Node js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18052215/

相关文章:

node.js - 即使用户注销后也保持 Node 处于运行状态

javascript - 用变量替换对象文字的属性

javascript - Mongoose 查询特定日期范围并获取空值

javascript - 如何使用预输入?

node.js - 从 Node 导出函数时出现 ReferenceError

node.js - Mongoose 和 async.parallel

mysql - Node : mysql: just can query maximum two times

javascript - 使用一个静态文件(动态)连接/缩小一组文件

javascript - (function () {//code })()的用法