Friday, 26 May 2017

system - Sleep not working properly

I'am a beginner in Node.js, while making one of service i came across this problem. 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.

 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 (callback) {
        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();
            });
            callback();
        }
    ], function (err) { //This function gets called after the two tasks have called their "task callbacks"
        if (err) return next(err);
        res.send("okay");
    });



via Ritesh Chauhan

No comments:

Post a Comment