I’m connecting to an API that requires quite a long response time and I tried to increase the timeout or rather make it perform another call when it returns 504,
import { fetch } from 'wix-fetch';
const MAX_RETRIES = 5; // Maximum number of retries
const RETRY_DELAY = 1000; // Delay between retries in milliseconds
const REQUEST_TIMEOUT = 30000; // Request timeout in milliseconds (30 seconds)
async function fetchWithRetries(url, options, retryCount = 0) {
try {
const response = await fetchWithTimeout(url, { ...options, timeout: REQUEST_TIMEOUT });
return response;
} catch (error) {
// Retry the request if the error is due to timeout or 504 status code and the maximum number of retries hasn't been reached
if (error.message === 'Request timed out' || error.status === 504) {
console.log(`Retrying request ${retryCount + 1} for URL: ${url}`);
await delay(RETRY_DELAY);
return fetchWithRetries(url, options, retryCount + 1);
}
throw error;
}
}
async function fetchWithTimeout(url, options, timeout = REQUEST_TIMEOUT) {
const fetchPromise = fetch(url, options);
const timeoutPromise = new Promise((_, reject) => {
setTimeout(() => reject(new Error('Request timed out')), timeout);
});
try {
const response = await Promise.race([fetchPromise, timeoutPromise]);
return response;
} catch (error) {
throw error;
}
}
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
it seems like the response time is always cap at :
14.53 s.
Is this a library fetch limitation/bug?
is there any alternative way to increase the timeout?