0

I wanted to be able to run two tasks inside async each function using the "async module".

for example:

async.each(openFiles, function( file, callback) {
    // task 1
    function(){
          callback();
     } 

    function(){
          callback(); // task 2, waits for task 1 to finish 
     }  
}, function(err){
   console.log("done");
});

Im using each because Im looping through each value and need apply two asks to each element.

5
  • can't you just perform the two tasks in this one function? Commented Aug 8, 2014 at 15:15
  • 1
    I'm not sure if I understand the question, but you can use async again nested inside the first async callback to run two tasks in parallel. Commented Aug 8, 2014 at 15:15
  • take a look at the other async functions, perhaps applyEach will do the trick, see also, applyEachSeries. Commented Aug 8, 2014 at 15:23
  • Why the downvote? this question seems legit... Commented Aug 8, 2014 at 15:30
  • Use either waterfall or series for your two inner functions. Not sure what the two current answers are attempting to do. Commented Aug 8, 2014 at 15:35

3 Answers 3

4

You should be able to run async.series inside async.each. This will iterate openfiles and run the series inside, it will only then progress through the each loop when series has finished.

async.each(openFiles, function(file, eachCallback) {
    async.series([

        function(seriesCallback) {
            seriesCallback();
        },
        function(seriesCallback) {
            seriesCallback();
        }
    ], function() {
        eachCallback();
    })
}, function(err) {
    console.log("done");
});
Sign up to request clarification or add additional context in comments.

Comments

0

Here is some code for the 2-async approach:

async.each(openFiles, function( file, callback) {

 async.each([task1, task2], function(task, cb) {

  task(file); // exec tasks 1, 2
  cb();       // one completed

 }, callback); // both completed

}, function(err){
 console.log("done");
});

1 Comment

Why use each and create an array each iteration? Why not use series?
0

You can make use of javascript callback over here to create dependency of task1 on task2. Something like this:

async.each(openFiles, function( file, callback) {
    // task 1
    function task1(function(){
          function task2 (function(){
              //callback of async
              callback();
          });
     }); 
}, function(err){
   console.log("done");
});

and your task 1 and task 2 function will take the callback as an argument something like this:

function task1(callback){
    //do whatever in task1
    callback();
}

function task2(callback){
    //do whatever in task1
    callback();
}

In this way task2 will run only when task1 is complete inside async.each

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.