JavaScript: 普通に同期処理
var defer = function(/* f1, f2, ... */){
var self = this, fn = Array.prototype.slice.call(arguments);
(function(/* arg2, arg3, ... */){
var args = Array.prototype.slice.call(arguments);
(fn.shift()).apply(self, [arguments.callee].concat(args));
})();
};
defer(function(next){
var t=1;
alert(t);
window.setTimeout(function(){ next(t+1); }, 1000); // 1秒後に第2引数を2として次の関数を呼ぶ
}, function(next,t){
alert(t);
window.setTimeout(function(){ next(t+1); }, 1000);
}, function(next,t){
alert(t);
window.setTimeout(function(){ next(t+1); }, 1000);
}, function(next,t){
alert(t);
});
前エントリのyieldっぽいことを配列とコールバック関数でやってみたメモ。
案外、これで十分かも。