Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request - awaitNextInterval #205

Open
jaysteiner-dev opened this issue Apr 12, 2024 · 3 comments
Open

Feature Request - awaitNextInterval #205

jaysteiner-dev opened this issue Apr 12, 2024 · 3 comments

Comments

@jaysteiner-dev
Copy link

jaysteiner-dev commented Apr 12, 2024

I'm using the p-queue for throttling, where potentially the API I am trying to hit could still rate limit me.
It would be great if there was a function I could use to handle those timeout after I catch the errors.

const queue = new PQueue({
    concurrency: 50,
    interval: 10000,
    intervalCap: 1000
    carryoverConcurrencyCount: true
});

const promises = myObjects.map(
  (obj) => async () =>
    myApiCall(obj.id).catch((err) => {
      if (err.code === HttpStatus.TOO_MANY_REQUESTS) {
        queue.awaitNextInterval()
      }
      this.logger.error(`failed with:`, err);
    })
);

await queue.addAll(promises);
@sindresorhus
Copy link
Owner

What exactly would queue.awaitNextInterval() do?

@gridagribnoy
Copy link

const promises = myObjects.map(obj => {
  const cb = async () => {
    return myApiCall(obj.id).catch(err => {
      if (err.code === HttpStatus.TOO_MANY_REQUESTS) {
        //queue.awaitNextInterval()
        return queue.add(cb, { priority: 999999 });
      }
      this.logger.error(`failed with:`, err);
    });
  };
  return cb;
});

await queue.addAll(promises);

@superbrobenji
Copy link

I have a package that could solve this, handles retries, throttling and timeouts, maybe give it a look: https://www.npmjs.com/package/asyncrify/v/0.1.19?activeTab=readme

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants