node.js async series function's arguments
Asked Answered
P

4

11

I need to do the code like following:

function taskFirst(k, v) {
    console.log(k, v);
}

function taskSecond(k, v) {
    console.log(k, v);
}

function run() {
    var g1 = "Something";
    var g2 = "Something";
    var g3 = "Something";
    var g4 = "Something";

    async.series(
        [
            taskFirst(g1, g2),
            taskSecond(g3, g4)
        ],
        function(error, result){

        }
    );
}

What is the right way to pass custom variables and async.js callback function?

Prithee answered 22/1, 2014 at 16:7 Comment(0)
P
20

You could do something like this:

function taskFirst(k, v, callback) {
    console.log(k, v);

    // Do some async operation
    if (error) {
        callback(error);
    } else {
        callback(null, result);
    }
}

function taskSecond(k, v, callback) {
    console.log(k, v);

    // Do some async operation
    if (error) {
        callback(error);
    } else {
        callback(null, result);
    }
}

function run() {
    var g1 = "Something";
    var g2 = "Something";
    var g3 = "Something";
    var g4 = "Something";

        async.series(
            [
                // Here we need to call next so that async can execute the next function.
                // if an error (first parameter is not null) is passed to next, it will directly go to the final callback
                function (next) {
                    taskFirst(g1, g2, next);
                },
                // runs this only if taskFirst finished without an error
                function (next) {
                    taskSecond(g3, g4, next);    
                }
            ],
            function(error, result){

            }
        );
}
Pustule answered 22/1, 2014 at 17:41 Comment(0)
B
3

This answer to async's github issue has worked for me perfectly. https://github.com/caolan/async/issues/241#issuecomment-14013467

for you it would be something like:

var taskFirst = function (k, v) {
   return function(callback){
      console.log(k, v);
      callback();
   }
};
Blim answered 20/1, 2015 at 2:2 Comment(2)
Thanks, I was missing that I needed to wrap the function and return it :)Diviner
I needed to create a dynamic array of functions with parameters that I needed to give to async.series and your answer helped me in the right direction, thanksKagera
V
2

It can be as follows

function taskFirst(k, v) {
    console.log(k, v);
}

function taskSecond(k, v) {
    console.log(k, v);
}

async.series([
    function(callback) { 
        callback(null, taskFirst(g1, g2));
    },
    function(callback) { 
        callback(null, taskFirst(g3, g4));
    }
],function(error, result){

});
Victualer answered 22/1, 2014 at 16:15 Comment(2)
This won't work. The callback is executed immediately so async will move on to the next function. You should pass the callback to your function and all execute it one your asynchronous work is complete.Alliaceous
Yes - this answer assumes the code inside taskFirst is blocking.Mcguigan
P
1

Better way.

const a1 = (a, callback) => {
    console.log(a, 'a1')
    callback()
}
const a2 = (a, callback) => {
    console.log(a, 'a2')
    callback()
}

const run = () => {
    async.series([
        a1.bind(null, 'asd'),
        a2.bind(null, 'asd2')
    ], () => {
        console.log('finish')
    })
}
run()
Prink answered 6/4, 2017 at 12:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.