title: async job scheduling in nodejs using loop
body: <p>I have a JSON of tasks grouped in the interval (see <code>unit</code>) they are supposed to be repeated by <code>every</code> interval of <code>unit</code>.</p>
  
  <pre><code> items=[
             { 
                  "unit" : "minute",
                  "detail" :
                   [ 
                      {"every" : 3, task : "task1"}
                      {"every" : 4, task : "task2"}...
                   ] 
             },
             { 
                  "unit" : "day",
                  "detail" :
                   [ 
                      {"every" : 3, task : "task3"}
                      {"every" : 4, task : "task4"}...
                   ] 
             },..
      ]
  </code></pre>
  
  <p>I used node-cron through following async function to schedule them on every <code>every</code> <code>unit</code> of intervals, like : task1 should happpen on every 3 minute.</p>
  
  <p>I tried this code :</p>
  
  <pre><code>var CronJob = require('cron').CronJob;
  function cronify(callback) 
  {
      for (var i in items)
      {
          for (var j in items[i].detail) {                        
              callback(items[i].unit,items[i].detail[j]);
          }
      }
  }
  
  cronify(function (duration, detail) {
      var when = '* * * * * *';
      switch(duration)
      {
          case 'minute' : 
              when = '* */'+detail.every+' * * * *';
              break;
          case 'hour' : 
              when = '* * */'+detail.every+' * * *';
              break;
          case 'day' :
              when = '* * * * */'+detail.every+' * *';
              break;
      }
      var job = new CronJob(when, function () {
          //Do the task for task
           }, null, true, "GMT");
      }        
  });
  </code></pre>
  
  <p>But the problem is every task gets executed every second independent of the duration mentioned via <code>unit</code> and <code>every</code>.</p>