Iteration with break
:
var attempt = 1;
while (true) {
var [succeeded, error] = await f()
.then(() => [true])
.catch(error => [false, error]);
attempt++;
if (succeeded || attempt > maxAttempts)
break;
console.log(` RETRY(${attempt}/${maxAttempts}): ${title}`);
}
GOTO:
retryTest:
var [succeeded, error] = await f()
.then(() => [true])
.catch(error => [false, error]);
if (error && attempt < maxAttempts) {
attempt++;
console.log(` RETRY(${attempt}/${maxAttempts}): ${title}`);
goto retryTest;
}
const tryTest = (attempt) => {
const [succeeded, error] = await f()
.then(() => [true])
.catch(error => [false, error]);
if (error && attempt < maxAttempts) {
console.log(` RETRY(${attempt}/${maxAttempts}): ${title}`);
return tryTest(attempt + 1);
}
return [succeeded, error];
};
tryTest(1);
Recursion with Y combinator:
const [succeeded, error] = Y((recurse, attempt = 1) => {
const [succeeded, error] = await f()
.then(() => [true])
.catch(error => [false, error]);
if (error && attempt < maxAttempts) {
console.log(` RETRY(${attempt}/${maxAttempts}): ${title}`);
return recurse(attempt + 1);
}
return [succeeded, error];
})();
With function keyword:
const [succeeded, error] = runWithRetries({attempt: 1, maxAttempts});
function runWithRetries(attempt) {
const [succeeded, error] = await f()
.then(() => [true])
.catch(error => [false, error]);
if (error && attempt < maxAttempts) {
console.log(` RETRY(${attempt}/${maxAttempts}): ${title}`);
return runTestWithRetries({attempt: attempt + 1, maxAttempts});
}
return [succeeded, error];
}