更多我有一个递归重试例行程序,这样的事情:防止绑定功能不止一次
Foo.prototype.retry = function(data, cb){
cb && (cb = cb.bind(this)); // want to bind the cb fn just once
this.resolutions[x] = (err, val) => {
if(val === 'foobar'){
// in this case, we do a retry
return this.retry(data, cb);
}
}:
}
,你可以看到,在某些情况下,我会再次调用this.run
重试。但我想避免多次拨打cb.bind()
。有没有什么好的方法可以做到这一点?
=>我的意思是,有没有办法以某种方式检查函数是否绑定到某个this
值?
只有很好的解决,我知道的是通过重试次数,就像这样:
Foo.prototype.retry = function(data, cb){
if(cb){
if(!data.__retryCount){
cb = cb.bind(this);
}
}
this.resolutions[x] = (err, val) => {
if(val === 'foobar'){
// we do a retry here
data.__retryCount = data.__retryCount || 0;
data.__retryCount++;
return this.retry(data, cb);
}
}:
}
为什么downvote?请解释你为什么不喜欢这个问题 –
是'cb? (cb = cb.bind(this))'有效的JS?我不知道你可以做一个三元没有一个替代定义 –
我的意思是cb &&不是cb?,对不起错字 –