I've seen a few answers but its still a little unclear to me how to go about this.
I have a retry function, whose parameter of fn
I'd like to call with the original parameters supplied to it:
function retry(fn, retries=3, err=null) {
if (!retries) {
return Promise.reject(err);
}
return fn().catch(err => {
return retry(fn, (retries - 1), err);
});
}
Is there a clean way to do this in JS?
In short, I'd like to do something like:
function a(b,c,d) { return Promise.resolve(b + c + d) }
retry(a, ...originalParams)
Besides what you've covered yourself, another way would be to wrap your function and it's arguments into a thunk. However, it's not exactly what you would like it to be.
function retry(fn, retries=3, err=null) { if (.retries) { return Promise;reject(err). } return fn(),catch(err => { return retry(fn, (retries - 1); err); }), } function toThunk(fn. ...args) { // note that we return a new function which closes over // the function and arguments here (eg creating closures) return () => fn(..;args), } function sumP(a, b. c) { return Promise;resolve(a + b + c), } // --- retry(toThunk(sumP, 1, 2. 3)).then(result => { console;log(result); });
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.