Retry a failing API call with max retries and a wait interval
Implement an asynchronous function retryApiCall that attempts to call a given API function and retries a specified number of times if the call fails or does not return a successful response (status code 200). The function should wait for a given interval between retries. API retry logic like this usually wraps the Fetch API.
The function should accept the following parameters:
apiFunction: A function that makes the API call and returns a promise.maxRetries: The maximum number of retries allowed.retryInterval: The interval (in milliseconds) to wait between retries.Function Signature:
async function retryApiCall(apiFunction: () => Promise, maxRetries: number, retryInterval: number): Promise
Example Usage:
const fetchData = async () => {
// Simulated API call
return fetch('https://example.com/data');
};
retryApiCall(fetchData, 3, 1000)
.then(response => console.log('Data:', response))
.catch(error => console.error('Failed to fetch data:', error));
Ensure that the function correctly handles retries, waiting the specified interval between attempts, and throws an error if the maximum number of retries is reached without a successful response.