async.waterfall只返回函数数组(node js)的array[0]索引处的函数的结果集

async.waterfall returns only the resultset of function at array[0] index of array of functions - node js

本文关键字:函数 array 索引 结果 返回 waterfall 数组 js node async      更新时间:2023-09-26

以下代码仅返回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库遵循error的常见Node.js模式-第一次回调。

为了表示任务"成功",第一个参数应为falsy(通常为null),任何数据都应作为第二个或之后的参数。

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

还要注意,async.waterfall()旨在将结果从一个任务传递到下一个任务,仅从最后一个任务(或error)到达result

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