Created
August 22, 2010 11:10
-
-
Save torgeir/543658 to your computer and use it in GitHub Desktop.
Understanding nodejs nextTick
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
var sys = require('sys'); | |
var print = function print(list) { | |
for (var i = 0; i < list.length; i++) { | |
var item = list[i]; | |
if (item.length) { | |
print(item); | |
} | |
else { | |
sys.puts(item); | |
} | |
} | |
}; | |
var printTick = function printTick(list, nextTick) { | |
var i = 0; | |
(function printNextItem() { | |
var item = list[i++]; | |
if (item) { | |
if (item.length) { | |
printTick(item, printNextItem); | |
} | |
else { | |
sys.puts(item); | |
process.nextTick(printNextItem); | |
if (list.length === i && nextTick) { | |
process.nextTick(nextTick); | |
} | |
} | |
} | |
})(); | |
}; | |
var l = [1, [2, [3, [4, 5], 6], 7], 8, 9, [10, 11]]; | |
print(l); | |
sys.puts('--'); | |
printTick(l); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment