Created
August 23, 2016 02:35
-
-
Save tzechienchu/908cbc407de7d9e8f4a2a3926cf08ff2 to your computer and use it in GitHub Desktop.
Async Loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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