Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function asyncLoop(iterations, func, callback) {
- var index = 0;
- var done = false;
- var loop = {
- next: function() {
- if (done) {
- //Return nothing if you call loop.next when loop is finished.
- return;
- }
- if (index < iterations) {
- index++;
- func(loop);
- } else {
- done = true;
- callback();
- }
- },
- iteration: function() {
- return index - 1;
- },
- break: function() {
- //If one query fails you want to prematurely stop the for loop and inform the client.
- done = true;
- callback();
- }
- };
- loop.next();
- return loop;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement