【发布时间】:2017-11-01 04:24:42
【问题描述】:
我有一个递归方法,它本质上是递增一个值直到达到最大值。
increase(delay) {
return new Promise((resolve, reject) => {
if (this.activeLeds < this.MAX_LEDS) {
this.activeLeds++
console.log(this.activeLeds)
}
return resolve()
}).delay(delay).then(() => {
if (this.activeLeds < this.MAX_LEDS) {
this.increase(delay)
} else {
return Promise.resolve()
}
})
}
我正在测试一些功能,我想知道increase() 方法何时完成(也就是解决)。
bounce(delay) {
return new Promise((resolve, reject) => {
this.increase(50).then(console.log('done'))
})
}
但是,我认为我在解决承诺之后做错了
this.activeLeds < this.MAX_LEDS
不再正确。我认为这与我没有解决最初的承诺有关,但我不知道如何解决它。
【问题讨论】:
-
避免
bounce中的Promiseconstructor antipattern!
标签: javascript recursion promise bluebird