async function fetchDataWithRetries(url, options = {}, retries = 3, delay = 1000) { for (let i = 0; i < retries; i++) { try { const response = await fetch(url, options); if (!response.ok) { // Handle specific API errors here if needed throw new Error(`HTTP error! status: ${response.status}`); } return await response.json(); } catch (error) { console.error(`Attempt ${i + 1} failed: ${error.message}`); if (i < retries - 1) { await new Promise(resolve => setTimeout(resolve, delay)); delay *= 2; // Exponential backoff } else { throw error; // Re-throw after last retry attempt } } } } // Usage example: // fetchDataWithRetries('https://api.example.com/data', { method: 'GET' }) // .then(data => console.log('Data fetched successfully:', data)) // .catch(error => console.error('Failed to fetch data after retries:', error));