Advertisement
Guest User

Untitled

a guest
Apr 16th, 2017
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function asyncLoop(iterations, func, callback) {
  2.     var index = 0;
  3.     var done = false;
  4.     var loop = {
  5.         next: function() {
  6.             if (done) {
  7.                 //Return nothing if you call loop.next when loop is finished.
  8.                 return;
  9.             }
  10.             if (index < iterations) {
  11.                 index++;
  12.                 func(loop);
  13.             } else {
  14.                 done = true;
  15.                 callback();
  16.             }
  17.         },
  18.         iteration: function() {
  19.             return index - 1;
  20.         },
  21.         break: function() {
  22.             //If one query fails you want to prematurely stop the for loop and inform the client.
  23.             done = true;
  24.             callback();
  25.         }
  26.     };
  27.     loop.next();
  28.     return loop;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement