0

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

double-happiness
  • 137
  • 1
  • 12
  • I don't see any debounce here, just a setTimeout, if i call the method two times it will make two calls at the same time, the calls should be 2500ms apart – Lk77 Aug 05 '22 at 12:07
  • The problem is that the debounce function doesn't return the result of the given function. In this case, it's `fetch` which returns a Promise and makes things a bit more complicated. – Chris Aug 05 '22 at 12:15
  • OK, that's looking pretty good, thanks. I will need to study that and try to understand how it works. Oddly, at the end of a longer input file, I got some error I've never seen before, relating to another part of the code, "uncaught (in promise) TypeError: e.join is not a function", but I didn't get it again for a smaller file. That seems to be the solution though, anyway. Thanks again. – double-happiness Aug 05 '22 at 12:43

0 Answers0