Wednesday, June 5, 2024
 Popular · Latest · Hot · Upcoming
170
rated 0 times [  177] [ 7]  / answers: 1 / hits: 9517  / 11 Years ago, wed, january 22, 2014, 12:00:00

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?


More From » node.js

 Answers
3

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){

}
);
}

[#48445] Tuesday, January 21, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rashawn

Total Points: 451
Total Questions: 83
Total Answers: 83

Location: Egypt
Member since Tue, May 3, 2022
2 Years ago
rashawn questions
;