Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Waterfall -> Parallel #5

Closed
ghost opened this issue Nov 29, 2010 · 3 comments
Closed

Waterfall -> Parallel #5

ghost opened this issue Nov 29, 2010 · 3 comments

Comments

@ghost
Copy link

ghost commented Nov 29, 2010

I want to run 2 functions in waterfall then pass the value from the last one to 3 functions in parallel.

I have been struggling with waterfall + parallel without any results.

Is this possible with async somehow?

@caolan
Copy link
Owner

caolan commented Nov 29, 2010

You probably want something like this:

async.waterfall([
    function (callback) {
        callback(null, 1);
    },
    function (arg, callback) {
        callback(null, arg1 + 1);
    },
    function (arg, callback) {
        async.parallel([
            function () { ... },
            function () { ... },
            function () { ... },
        ], callback);
    }
]);

Hope that helps :)

@ghost
Copy link
Author

ghost commented Nov 29, 2010

Yeah it worked!

But one problem when I returned a closure like this:

function printName(name) {
    return function(callback) {
        console.log(arg1);
        console.log(name);
        callback(null, name);
    }
}

async.waterfall([
    function (callback) {
        callback(null, 1);
    },
    function (arg, callback) {
        callback(null, arg1 + 1);
    },
    function (arg, callback) {
        async.parallel([
            printName("Josh"),
            function (callback) { ... },
            function (callback) { ... },
        ], finalCallback);
    }
]);

It said arg1 is not defined because it's in the closure now and not inside async.waterfall().

This forces the code to not stay DRY cause I can't reuse functions.

Is there a way to fix this?

@caolan
Copy link
Owner

caolan commented Nov 29, 2010

You'll need to pass arg1 as an argument to printName... it looks like that would make sense anyway if you want to reuse printName.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant