我有一系列的承诺一个接一个地运行。
var Sequence = Backbone.Collection.extend({
model: Timer,
_sequence() {
return this.reduce((promise,model)=>{
return promise.then(()=>{
return model.start(); // return a Promise
});
}, Promise.resolve());
},
start(count = 1) {
// this sequence must be repeated for n times, where n is at least one
return this._sequence();
}
});
该模型是一个计时器。当我调用 model.start()时,它会返回一个在计时器到期时将履行的承诺。
如何重复该序列以便我可以
var s1 = new Sequence([timer1, timer2, timer3]);
s1.start(2).then(function(){
// the sequence was repeated 2 times
});
任何提示?感谢。