Medium
How would you complete this function to implement a retry mechanism with exponential backoff for failed API calls?
1async function fetchWithRetry(url, options = {}, maxRetries = 3) {2 let retryCount = 0;3 let lastError = null;45 const calculateBackoff = (attempt) => {6 return Math.min(1000 * Math.pow(2, attempt), 10000);7 };89 while (retryCount <= maxRetries) {10 try {11 _______________________________12 _______________________________13 _______________________________14 return response;15 } catch (error) {16 lastError = error;17 if (retryCount === maxRetries) break;1819 const backoffTime = calculateBackoff(retryCount);20 retryCount++;2122 await new Promise(resolve => setTimeout(resolve, backoffTime));23 }24 }2526 throw new Error(`Failed after ${maxRetries} retries: ${lastError.message}`);27}