/**
* Retries an API operation with fallback logic for validation checks.
*
* @param {function} apiCall - The function that performs the API call. Should return a Promise.
* @param {number} maxRetries - The maximum number of retry attempts.
* @param {number} delay - The delay (in milliseconds) between retries.
* @param {function} fallbackValidation - A function to perform validation checks if the API call fails after retries.
* @returns {Promise} A Promise that resolves with the API response or rejects with the fallback validation result.
*/
async function retryApiCall(apiCall, maxRetries, delay, fallbackValidation) {
let attempts = 0;
while (attempts < maxRetries) {
try {
const response = await apiCall(); // Make the API call
return response; // Success, return the response
} catch (error) {
attempts++;
if (attempts === maxRetries) {
// Last retry, use fallback validation
return await fallbackValidation(error);
}
// Wait before retrying
await new Promise(resolve => setTimeout(resolve, delay));
}
}
}
export default retryApiCall;
Add your comment