更多我有一個遞歸重試例行程序,這樣的事情:防止綁定功能不止一次
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?,對不起錯字 –