Last active
December 10, 2015 13:18
-
-
Save tatat/4439621 to your computer and use it in GitHub Desktop.
いっこづつ
This file contains hidden or 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 Step = function(steps) { | |
if (this instanceof Step) { | |
this.items = []; | |
this.oncomplete = null; | |
this.onabort = null; | |
if (steps) { | |
for (var i = 0, j = steps.length; i < j; i ++) | |
this.add(steps[i]); | |
} | |
} else { | |
return new Step(steps); | |
} | |
}; | |
Step.prototype.add = function(fn) { | |
this.items.push(fn); | |
return this; | |
}; | |
Step.prototype.on = function(type, fn) { | |
if (fn) this['on' + type] = fn; | |
return this; | |
}; | |
Step.prototype.run = function(complete, abort) { | |
var self = this | |
, items = this.items; | |
(function wrapper(data) { | |
var item = items.shift(); | |
if (!item) { | |
self.oncomplete && self.oncomplete.call(self, data); | |
complete && complete.call(self, data); | |
} else { | |
item.call(self, function(err) { | |
if (err instanceof Error) { | |
self.onabort && self.onabort.call(self, err, data); | |
abort && abort.call(self, err, data); | |
} else { | |
wrapper.call(self, data); | |
} | |
}, data); | |
} | |
}).call(this, {}); | |
return this; | |
}; |
This file contains hidden or 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
new Step().add(function(next, data) { | |
console.time('step1'); | |
data.nyan1 = 'nyan1'; | |
setTimeout(next, 1000); | |
}).add(function(next, data) { | |
data.nyan2 = 'nyan2'; | |
setTimeout(next, 1000); | |
}).add(function(next, data) { | |
data.nyan3 = 'nyan3'; | |
setTimeout(next, 1000); | |
}).add(function(next, data) { | |
data.nyan4 = 'nyan4'; | |
setTimeout(next, 1000); | |
}).add(function(next, data) { | |
data.nyan5 = 'nyan5'; | |
setTimeout(next, 1000); | |
}).run(function(data) { | |
console.timeEnd('step1'); | |
console.log(data); | |
new Step().add(function(next, data) { | |
console.time('step2'); | |
data.nyan1 = 'nyan1'; | |
setTimeout(next, 1000); | |
}).add(function(next, data) { | |
data.nyan2 = 'nyan2'; | |
setTimeout(next, 1000); | |
}).add(function(next, data) { | |
// data.nyan3 = 'nyan3'; | |
// setTimeout(next, 1000); | |
next(new Error('error!')); // Error | |
}).add(function(next, data) { | |
data.nyan4 = 'nyan4'; | |
setTimeout(next, 1000); | |
}).add(function(next, data) { | |
data.nyan5 = 'nyan5'; | |
setTimeout(next, 1000); | |
}).run(function(data) { | |
console.timeEnd('step2'); | |
console.log(data); | |
}, function(err, data) { | |
console.timeEnd('step2'); | |
console.log(data); | |
console.error(err); | |
}); | |
}, function(err, data) { | |
console.timeEnd('step1'); | |
console.log(data); | |
console.error(err); | |
}); | |
// step1: 5010ms | |
// { nyan1: 'nyan1', | |
// nyan2: 'nyan2', | |
// nyan3: 'nyan3', | |
// nyan4: 'nyan4', | |
// nyan5: 'nyan5' } | |
// step2: 2010ms | |
// { nyan1: 'nyan1', nyan2: 'nyan2' } | |
// [Error: error!] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment