async_caller.d.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. type ResponseCallback = (response?: Response) => Promise<boolean>;
  2. export interface AsyncCallerParams {
  3. /**
  4. * The maximum number of concurrent calls that can be made.
  5. * Defaults to `Infinity`, which means no limit.
  6. */
  7. maxConcurrency?: number;
  8. /**
  9. * The maximum number of retries that can be made for a single call,
  10. * with an exponential backoff between each attempt. Defaults to 6.
  11. */
  12. maxRetries?: number;
  13. onFailedResponseHook?: ResponseCallback;
  14. debug?: boolean;
  15. }
  16. export interface AsyncCallerCallOptions {
  17. signal?: AbortSignal;
  18. }
  19. /**
  20. * A class that can be used to make async calls with concurrency and retry logic.
  21. *
  22. * This is useful for making calls to any kind of "expensive" external resource,
  23. * be it because it's rate-limited, subject to network issues, etc.
  24. *
  25. * Concurrent calls are limited by the `maxConcurrency` parameter, which defaults
  26. * to `Infinity`. This means that by default, all calls will be made in parallel.
  27. *
  28. * Retries are limited by the `maxRetries` parameter, which defaults to 6. This
  29. * means that by default, each call will be retried up to 6 times, with an
  30. * exponential backoff between each attempt.
  31. */
  32. export declare class AsyncCaller {
  33. protected maxConcurrency: AsyncCallerParams["maxConcurrency"];
  34. protected maxRetries: AsyncCallerParams["maxRetries"];
  35. queue: typeof import("p-queue")["default"]["prototype"];
  36. private onFailedResponseHook?;
  37. private debug?;
  38. constructor(params: AsyncCallerParams);
  39. call<A extends any[], T extends (...args: A) => Promise<any>>(callable: T, ...args: Parameters<T>): Promise<Awaited<ReturnType<T>>>;
  40. callWithOptions<A extends any[], T extends (...args: A) => Promise<any>>(options: AsyncCallerCallOptions, callable: T, ...args: Parameters<T>): Promise<Awaited<ReturnType<T>>>;
  41. fetch(...args: Parameters<typeof fetch>): ReturnType<typeof fetch>;
  42. }
  43. export {};