This is the most relevant code in this case, but I can post the entirety if requested:
function debounce(func, timeout = 1000){
let timer;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => { func.apply(this, args); }, timeout);
};
}
const throttleFetch = debounce(fetch,2500);
function getRelease(idFiltered) {
return throttleFetch(`https://api.[...]
The previous version had the following instead, but didn't throw the same error:
const timer = ms => new Promise(resolve => setTimeout(resolve, ms));
const createThrottler = (limitHeader) => {
let requestTimestamp = 0;
let rateLimit = 0;
return (requestHandler) => {
return async (...params) => {
const currentTimestamp = Number(Date.now());
if (currentTimestamp < requestTimestamp + rateLimit) {
const timeOut = rateLimit - (currentTimestamp - requestTimestamp);
requestTimestamp = Number(Date.now()) + timeOut;
await timer(timeOut)
}
requestTimestamp = Number(Date.now());
const response = await requestHandler(...params);
if (!rateLimit > 0) {
rateLimit = limitHeader;
}
console.log(limitHeader);
console.log(rateLimit);
return response;
}
}
}
const throttle = createThrottler("60");
const throttleFetch = throttle(fetch);
function getRelease(idFiltered) {
return throttleFetch(`[...]
So I don't understand why throttleFetch
is working OK in the latter version, but not in the former? Any help please? TIA