Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

NodeJs Async.waterfall within Node-Webkit

NodeJs Async.waterfall within Node-Webkit

Problem

I'm trying to use the nodejs async module (waterfall method) within a node-webkit app, however it does not run properly. It seems to jump straight to the last item in the chain and ignore everything else. My code works perfectly in nodejs though. I thought it could be my code, but even the following example from the async docs does the same.

 async.waterfall([
    function(callback){
        callback(null, 'one', 'two');
    },
    function(arg1, arg2, callback){
        callback(null, 'three');
    },
    function(arg1, callback){
        // arg1 now equals 'three'
        callback(null, 'done');
    }
], function (err, result) {
    console.log(result)
    // result now equals 'done'
});

However the following example that uses the async.series methods does work:

async.series([
function(callback){
    // do some stuff ...
    callback(null, 'one');
},
function(callback){
    // do some more stuff ...
    callback(null, 'two');
}
],
// optional callback
function(err, results){
// results is now equal to ['one', 'two']
});
Problem courtesy of: user1513388

Solution

This is a bug with the async module, and there's an open issue on Github about this (issue #302.) The trouble is when node creates a new environment (using the vm module), the array checks async uses internally are invalid.

I fixed this by changing the Array check on line 472 in lib/async.js from:

async.waterfall = function (tasks, callback) {
    callback = callback || function () {};
    if (tasks.constructor !== Array) {

to:

async.waterfall = function (tasks, callback) {
    callback = callback || function () {};
    if (!Array.isArray(tasks)) {

There are a few pull requests open to fix this issue, so hopefully this will be fixed soon.

Solution courtesy of: theabraham

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

NodeJs Async.waterfall within Node-Webkit

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×