Last active
August 29, 2015 14:13
-
-
Save thealexbaron/ae40df9bb0a642c124bd to your computer and use it in GitHub Desktop.
Extremely Basic Asynchronous Loop
This file contains hidden or 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
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