Skip to content

Instantly share code, notes, and snippets.

@kessler
Created July 4, 2014 15:07
Show Gist options
  • Save kessler/e6858d0b76169277b347 to your computer and use it in GitHub Desktop.
Save kessler/e6858d0b76169277b347 to your computer and use it in GitHub Desktop.
async recursion example
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