Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@torgeir
Created August 22, 2010 11:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save torgeir/543658 to your computer and use it in GitHub Desktop.
Save torgeir/543658 to your computer and use it in GitHub Desktop.
Understanding nodejs nextTick
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