Search code examples
javascriptnode.jsnode-async

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


The following code returns only the resultset of 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);
    });
]);

Solution

  • The async library follows the common Node.js pattern of error-first callbacks.

    To signify that the task was "successful," the 1st argument should be falsy (typically null) with any data as the 2nd or after argument.

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

    Also note that async.waterfall() is intended for passing a result from one task to the next, arriving at just the result from the final task (or an error).

    If you want to collect results from each task, try async.series(). With it, result will be an Array of the data passed from each task.