-1

I need to call a function after async.forEachOfSeries completes it's task. I have an Object named rows. It contains postId and other information. I need to make a request inside the funtion abc() to get comments of that particular post and save them in database. If crawling is done then I call callback() to make request for the next post in rows. When loop is finished, I need to call the function xyz(). Here's a sample code:

function loopTest(){
    async.forEachOfSeries(rows, function (que, key, callback) {
        function abc(){
            //make request to facebookto get comments and stores in database
        }
        if(!commentCrawlingDone)
        {
            abc();
        }
        else{
            callback();
        }
    });

    xyz();
}

Araf
  • 189
  • 3
  • 12

1 Answers1

1

https://caolan.github.io/async/v3/docs.html#eachOfSeries

You're currently passing two arguments to .eachOfSeries; looks like you can pass xyz as a third argument.

(rows is your first argument, and your anonymous function that takes (que, key, callback) is your second argument.)

So, instead of

...
  });

  xyz();

you would write

...
  }, xyz);
dmitrydwhite
  • 542
  • 2
  • 7
  • Or, since in the docs it also says "Returns: a promise, if a callback is omitted", you could write ... }).then(xyz); – dmitrydwhite Nov 26 '19 at 06:27
  • Thanks, i was looking for this. I'm new in Stackoverflow that's why my upvote is not showing up. thanks a lot – Araf Nov 26 '19 at 06:34