[英]Javascript recursive promise stuck in infinite loop
I have created a recursive promise function: 我创建了一个递归承诺函数:
this.testFunction = Bluebird.method(function (instanceID) {
var object = this;
return object.canSsh(instanceID)
.then(function (sshable) {
if (sshable) {
return object.onSshable(instanceID)
.then(function () {
return Bluebird.resolve();
});
}
else {
return Bluebird.delay((SSH_POLLING_INTERVAL * 1000))
.then(function () {
return object.testFunction(instanceID);
});
}
})
.catch(function (err) {
return Bluebird.reject(err);
});
});
However, even when sshable
becomes true, this function keeps recursively going on and on in an infinite loop. 但是,即使
sshable
变为true,此函数也会在无限循环中继续递归进行。 I expected that once I return from the sshable
block, it should exist the function. 我希望一旦我从
sshable
块返回,它就应该存在该函数。
Even when
sshable
becomes true, this function keeps recursively going on and on in an infinite loop.即使
sshable
变为true,此函数sshable
在无限循环中不断递归进行。
I cannot reproduce. 我无法复制。 It seems that something else is calling your your
testFunction
, not the recursive call from that delayed then
callback. 似乎还有其他东西正在调用您的
testFunction
,而不是从延迟then
回调的递归调用。
Regardless, you can heavily simplify your function: 无论如何,您可以大大简化您的功能:
this.testFunction = function(instanceID) {
return this.canSsh(instanceID).then(function(sshable) {
if (sshable) {
return this.onSshable(instanceID);
} else {
return Bluebird.delay(SSH_POLLING_INTERVAL * 1000)
.then(this.testFunction.bind(this, instanceID));
}
}.bind(this))
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.