function hoge(name, count) { var i = 0; (function fuga() { if (i < count) { console.log(name + ":" + i++); process.nextTick(fuga); } else { console.log(name + ":end"); } })();
みたいにクロージャでnextTickに値を渡す方法だけど、関数なのだから普通に引数で渡したい。そんな時にはbindを使う。
function hoge(name, count) { (function fuga(i) { if (i < count) { console.log(name + ":" + i); process.nextTick(fuga.bind(null, i+1)); } else { console.log(name + ":end"); } })(0); } hoge("a", 10); hoge("b", 10);
fuga関数も要らない
function hoge(name, count, i) { if (i === undefined) i = 0; if (i < count) { console.log(name + ":" + i); process.nextTick(hoge.bind(null, name, count, i+1)); } else { console.log(name + ":end"); } } hoge("a", 10); hoge("b", 10);
すっきりしました。