0

this block of code process else block first and then if block, for all request at once, i need a code that execute 5 iterations stop for 15 sec. and then again continue from next 5 iterations and so on.

function passotken(token, callback) {


async.waterfall([
function(callback) {
db.executesql("select top 20 ext_id as EMPNUM ,  data as datae from newtable", function (data, err) {

    callback(null, data);
    });
},
function(data, callback) {
    var _json_parse = JSON.parse(JSON.stringify(data));
    var rows = data.length;

    console.log(rows)
    var cnt = 1;

    for (var row = 1; row <= rows; row++) {

    logger.info(_json_parse[row-1].EMPNUM);
    //console.log(dateFormat(_json_parse[row].datae));                      
    var req = 'https://pratik.com/ta/rest/v2/companies/|RHV/employees/|' +_json_parse[row-1].EMPNUM + '/timesheets?date=' + dateFormat(_json_parse[row-1].datae, "isoDate");
    //console.log(req);
    var myXMLText = req;

    reques.push(myXMLText);

    }
   // console.log(reques);

    for (var a = 0; a < rows; a++) {
         //CURRENTLY PROCESSING ALL REQUEST IN IF BLOCK AND STOP FOR 15 SEC FOR ONLY FIRST IF CONDITION AFTER THAT ALL IF CONDITION PROCESSING WITHOUT HALT
    if(a%5==0)
    {
        console.log("if");
        //console.log(reques[a]);

        //postreq(reques[a],token,sleeped(a));
      /*  setTimeout(function(){sleeped(reques[a],token);;

    },15000); */
        sleeped(reques[a],token);


        //SHOULD PROCESS IF BLOCK ONCE AND STOP FOR 15 SEC
    }
    else
    {

        postreqELSE(reques[a],token);
        //SHOULD PROCESS ALL REQUECT IN ELSE BLOCK TILL IF CONDITION ABOVE NOT SATISY

    }

    }

Promise.all(ps)
.then((results) => {
console.log("results"); // Result of all resolve as an array
}).catch(err => console.log("err")); 
},
], function(err, result) {
if (!err)
    console.log("Successfully completed")
else console.log(err);
});
};

function callback() {
console.log("completed successfully");
}

function postreq(request1,token)
{
//BLOCK

    }

    function sleeped(requesarr,token)
    {
    console.log("in sleeping");
    //console.log(requesarr,token)

    setTimeout(function(){
        postreq(requesarr,token);
    },15000);}


    function postreqELSE(request2,token1)
{
console.log("in 3RD function");
   //BLOCK2

    }
  • The code you provided is too long, please make a minimal example code to reproduce your issue. – xerq Jun 08 '17 at 11:21
  • want to co control the execution at for loop for (var a = 0; a < rows; a++) { if(a%5==0) { console.log("if"); //console.log(reques[a]); //postreq(reques[a],token,sleeped(a)); /* setTimeout(function(){sleeped(reques[a],token);; },15000); */ sleeped(reques[a],token); //postreq(reques[a],token,sleeped(a)); } else { console.log("else"); //console.log(reques[a]); //postreq(reques[a],token,sleeped(a)); postreqELSE(reques[a],token); } } – pratik mokal Jun 08 '17 at 11:46
  • using `Promish` lib and counter you can archive this – Divyesh Kanzariya Jun 08 '17 at 12:08

1 Answers1

0

Sure, this question was answered previously here: What is the JavaScript version of sleep()?

Following is an example which will do what you're requesting:

var reques = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}
async function checkLoop()
{
  for (var a = 0; a < 20; a++)
  { console.log("["+a+"] a%5 is: "+a%5);
    console.log ( new Date().toLocaleTimeString());
    if(a%5==0)
    { console.log("if");
    console.log ( new Date().toLocaleTimeString());
    await sleep(15000);
    }
    else
    { console.log("else");
    console.log ( new Date().toLocaleTimeString());

    }
  }
}
checkLoop();

This prints out the following: (partial) Important to note that it will always pause on the first iteration because the index starts at 0, not 1.

[0] a%5 is: 0  
8:58:38 AM  
if  
[1] a%5 is: 1  
8:58:53 AM  
else  
8:58:53 AM  
[2] a%5 is: 2  
8:58:53 AM  
else  
8:58:53 AM  
[3] a%5 is: 3  
8:58:53 AM  
else  
8:58:53 AM  
[4] a%5 is: 4  
8:58:53 AM  
else  
8:58:53 AM  
[5] a%5 is: 0  
8:58:53 AM  
if  
8:58:53 AM  
[6] a%5 is: 1  
8:59:08 AM  
else  
8:59:08 AM  
[7] a%5 is: 2  
8:59:08 AM  
else  
8:59:08 AM  

If you can't use async, then try this approach: (interval set at 1.5 secs for easier testing. just change the 1500 to 15000 to = 15 seconds)

var reques = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
var _interval;
var timeToWait = 1500;
function checkLoop(_init)
{
  clearInterval(_interval);
  console.log("checkLoop entered with value of: ", _init);
  var start = (typeof(_init) == "undefined") ? 0 : _init;
  console.log("start is: "+start);
  for (var a = start; a < 20; a++)
  { console.log("["+a+"] a%5 is: "+a%5);
    console.log ( new Date().toLocaleTimeString());
    if(a%5==0)
    { console.log("if");
    console.log ( new Date().toLocaleTimeString());
    (function(_idx){_interval = setInterval(function(){checkLoop(_idx+1)}, timeToWait);})(a)
    break;
    }
    else
    { console.log("else");
    console.log ( new Date().toLocaleTimeString());

    }
  }
}
checkLoop();
Bob Dill
  • 1,000
  • 5
  • 13
  • Thanks for update, but this Async prefix work in node for v7 and above, and babel also unable to help us. – pratik mokal Jun 08 '17 at 15:11
  • Answer updated to provide alternate approach avoiding use of async prefix. uses setInterval and clearInterval to handle timing and break to exit the current loop. recursively calls itself until loop is exhausted. – Bob Dill Jun 08 '17 at 19:04
  • If this is the right answer for you, please tag it. thanks. – Bob Dill Jun 08 '17 at 19:22