Best practice is to avoid the promise constructor anti-pattern. Basically, new Promise
exists mostly to wrap non-promise APIs, so if your functions already return promises, then there's usually a way to avoid using it.
If you're doing a low fixed number retries, then your case is as simple as:
function ayncFunc() {
return doAsync().catch(doAsync).catch(doAsync).catch(doAsync);
};
For a configurable number of retries, you'd expand this to:
var retries = 3;
function ayncFunc() {
var p = doAsync();
for (var i = 0; i < retries; i++) {
p = p.catch(doAsync);
}
return p;
};
Or for higher number of retries, you could use a recursive approach:
function ayncFunc() {
function recurse(i) {
return doAsync().catch(function(e) {
if (i < retries) {
return recurse(++i);
}
throw e;
});
}
return recurse(0);
};
var console = { log: msg => div.innerHTML += msg + "<br>" };
function doAsync() {
console.log("doAsync");
return Promise.reject("Nope");
}
function ayncFunc() {
return doAsync().catch(doAsync).catch(doAsync).catch(doAsync);
};
var retries = 3;
function ayncFunc2() {
var p = doAsync();
for (var i=0; i < retries; i++) {
p = p.catch(doAsync);
}
return p;
};
function ayncFunc3() {
function recurse(i) {
return doAsync().catch(function(e) {
if (i < retries) {
return recurse(++i);
}
throw e;
});
}
return recurse(0);
};
ayncFunc().catch(function(e) { console.log(e); })
.then(ayncFunc2).catch(function(e) { console.log(e); })
.then(ayncFunc3).catch(function(e) { console.log(e); });
<div id="div"></div>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…