1

I want to store all the data fetched from the database in arr_obj, then use this variable int async.forEachLimit function. For this reason i used async.series function, everything works fine except sleep(1000), code sleeps as soon as second function of async.series is called and then gives all the result together.Being a beginner in NodeJs i don't have much idea about all this.

var sleep = require('system-sleep');
//
//
 var arr_obj = [];
    async.series([
        function (callback) {
            Service.listAllUser(req.body, function (err, data) {
                if(err) return callback(err);
                arr_obj = data.toJSON();
                callback();
            });
        },
        function (callback1) {
        console.log(arr_obj);
            async.forEachLimit(arr_obj, 1, function (item, callback) {
                Quality_Service.qualityService(item, function (err, data) {
                    if (err) return next(err);
                    console.log(data);
                });
                sleep(1000);
                callback();
            });
            callback1();
        }
    ], function (err) { //This function gets called after the two tasks have called their "task callbacks"
        if (err) return next(err);
        res.send("okay");
    });
  • What does "give all the result together" mean? Also, it sounds like this works "as designed" since it *should* sleep right after the second function is called. Then, it might be a good idea to use something other than "callback" as the name of the callback of the forEachLimit `function`. Finally, I'm wondering what is the actual question here: what are you expecting? – Gyuri May 26 '17 at 16:19
  • try to use set timeout and use counter to see if its passing through the sleep – Mr X May 26 '17 at 16:20
  • @Gyuri I want that the program should sleep for 1000ms every time after function Quality_Service.qualityService, instead it sleeps for some time as soon as the 2nd function in async.series is called. – Ritesh Chauhan May 26 '17 at 16:28
  • What do you use for `sleep()`? One option is to use the same logic as used in the example for async/series: a `setTimeout` instead of `sleep` before (or rather around) the callback of the async, in your case callback1: http://caolan.github.io/async/docs.html#series – Gyuri May 26 '17 at 16:35
  • when i'am not using async.series and directly passing the arr_obj inside the function Quality_Service.qualityService then sleep() wait's for specified time after each time the function is called. – Ritesh Chauhan May 26 '17 at 16:37
  • I've already tried to use SetTimeout function but still same problem persists reference: https://stackoverflow.com/a/30515700/8071194 – Ritesh Chauhan May 26 '17 at 16:40

1 Answers1

0

Try to use callback inside the foreach, with setTimeout

async.forEachLimit(arr_obj, function (item, callback) {
            Quality_Service.qualityService(item, function (err, data) {
                if (err) return next(err);
                setTimeout(callback, 1000, err);
            });                
        });
Mr X
  • 1,637
  • 3
  • 29
  • 55