如何限制递归退出 X 次的承诺

问题描述 投票:0回答:0

我正在尝试编写一个将由反应组件使用的承诺。我正在使用

lodash
进行节流。 这个想法是使用这个函数的组件应该尽可能多地透明地调用这个函数,而不会导致太多调用。

let throttledApi = throttle(() => {
    // tracking number of attempts
  let attempts = 0;
  const retryableFn = () => {
    // do I need a return before api here?
    api()
      .then((response) => {
        return response;
      })
      .catch((error) => {
        attempts++;
        if (attempts === 3) throw error; // throw to the component using this functino
                // same question as above for below line
        else retryableFn();
      });
  };
}, 60 * 1000); // 1 minute


// I am using it like this
throttledFn = throttledApi();
// How do I properly use the throttled instance
// How do I try/catch properly.
// Is this thenable?
throttledFn();

javascript async-await promise throttling
© www.soinside.com 2019 - 2024. All rights reserved.