JavaScript でエラーになったらリトライするスクリプト
わりとよくあるのが、API などの通信でエラーになった場合に、リトライする処理を入れたいというケース。
まぁ、さくっと書けるだろうけどメモとして残しておく。
async function retryAsyncFunction(asyncFunc, maxRetries = 3, delay = 1000, isDebug = false) {
let attempts = 0;
while (attempts < maxRetries) {
try {
return await asyncFunc();
} catch (error) {
attempts++;
if (isDebug) {
console.error(`Attempt ${attempts} failed: ${error.message}`);
}
if (attempts >= maxRetries) {
throw new Error(`Max retries reached: ${error.message}`);
}
await new Promise(resolve => setTimeout(resolve, delay));
}
}
}
asyncFunc: リトライしたい非同期関数maxRetries: 最大リトライ回数(デフォルトは 3 回)delay: リトライ間の待機時間(ミリ秒、デフォルトは 1000 ミリ秒)isDebug: デバッグモード(true の場合、リトライの試行回数とエラー内容をコンソールに出力する)
まぁデバッグモードなんていらないんだけどね。