Created
July 4, 2014 15:07
-
-
Save kessler/e6858d0b76169277b347 to your computer and use it in GitHub Desktop.
async recursion example
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 count = 0 | |
var countWithDelay = 0 | |
function recurse() { | |
console.log('recurse: %d', count) | |
if (count++ === 100) return | |
setImmediate(recurse) | |
} | |
function recurseWithDelay() { | |
console.log('recurseWithDelay: %d', countWithDelay) | |
if (countWithDelay++ === 100) return | |
// recurse a second from now | |
setTimeout(recurseWithDelay, 1000) | |
} | |
function recurseInternalState(state) { | |
console.log('recurseInternalState: %d', state) | |
if (state++ === 100) return | |
setImmediate(function () { | |
recurseInternalState(state) | |
}) | |
} | |
recurse() | |
recurseWithDelay() | |
recurseInternalState(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment