Skip to content

Instantly share code, notes, and snippets.

@tzechienchu
Created August 23, 2016 02:35
Show Gist options
  • Save tzechienchu/908cbc407de7d9e8f4a2a3926cf08ff2 to your computer and use it in GitHub Desktop.
Save tzechienchu/908cbc407de7d9e8f4a2a3926cf08ff2 to your computer and use it in GitHub Desktop.
Async Loop
function asyncLoop(iterations, func, callback) {
var index = 0;
var done = false;
var loop = {
next: function() {
if (done) {
return;
}
if (index < iterations) {
index++;
func(loop);
} else {
done = true;
callback();
}
},
iteration: function() {
return index - 1;
},
break: function() {
done = true;
callback();
}
};
loop.next();
return loop;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment