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

StackOverflow https://stackoverflow.com/questions/18052215

  •  23-06-2022
  •  | 
  •  

Question

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);
    });
]);
Was it helpful?

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top