Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

async.waterfall mechanism doesn't seem to work with another level of nested callback #130

Closed
pjdurai opened this Issue · 2 comments

4 participants

@pjdurai

Hi

I am using async in iOS using phonegap.

This is the canonical example for waterfall.

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

});
}

Suppose I have to call an asynchronous function in one of the array member functions, it doesn't work as I expected.

This is what I came up with. Please tell me if I am missing anything. Or is it not expected to work in such a manner?
e.g.

MyModule.testMe = function(event) {
async.waterfall([
function(callback){
object.anotherfn( function (success_result) { callback(null, success_result);}, // This doesn't work.
function(error) { callback(error));
},
function(some_result_object, callback){
callback(null, 'three');
},
function(arg1, callback){
callback(null, 'done');
}
], function (err, result) {
// result now equals 'done'

});
}

Should the 'callback' at the end of the top-level in each array member function?
Is there any way around this?

Thanks
pj

@ichernev

Your 'non working code' when prettified looks like this:

MyModule.testMe = function (event) {
    async.waterfall([

    function (callback) {
        object.anotherfn(function (success_result) {
            callback(null, success_result);
        }, // This doesn't work.
        function (error) {
            callback(error));
        }, function (some_result_object, callback) {
            callback(null, 'three');
        }, function (arg1, callback) {
            callback(null, 'done');
        }], function (err, result) {
        // result now equals 'done'
    });
    }

but I think it should look like this instead:

MyModule.testMe = function (event) {
    async.waterfall([

    function (callback) {
        object.anotherfn(function (success_result) {
            callback(null, success_result);
        }, // This doesn't work.
        function (error) {
            callback(error));
        });
    }, function (some_result_object, callback) {
        callback(null, 'three');
    }, function (arg1, callback) {
        callback(null, 'done');
    }], function (err, result) {
        // result now equals 'done'
    });
}
@caolan caolan closed this
@alizbazar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.