Skip to content

Instantly share code, notes, and snippets.

@thealexbaron
Last active August 29, 2015 14:13
Show Gist options
  • Save thealexbaron/ae40df9bb0a642c124bd to your computer and use it in GitHub Desktop.
Save thealexbaron/ae40df9bb0a642c124bd to your computer and use it in GitHub Desktop.
Extremely Basic Asynchronous Loop
var iterationCount = 3;
function doIterations(cb) {
if (iterationCount !== 0) {
// print the number of iterations, then wait 5 seconds
setTimeout(function(){ console.log(iterationCount); iterationCount --; doIterations(cb); }, 500);
}
else {
// now that we're done, fire the callback (if one exists)
cb && cb();
}
}
doIterations(function() { console.log('donezo!'); });
// prints:
// 3
// 2
// 1
// donezo!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment