Skip to content

Create API for retrying throwed functions and rejected promises #96

@parzhitsky

Description

@parzhitsky

Related: #62 #76

Think about something like:

// `fn` is a function that either: returns a non-promise result; returns a pending/resolved/rejected promise; throws error;
const result = await retry.action(fn, {
  promiseTimeout: 1000,
  retryDelay: 2000,
  maxCount: 10,
  onMaxCountExceeded: (resolve, reject, retry) => {
    // ...
  },
});
declare namespace retry {
  function action<Result>(fn: (...args: never[]) => Result | PromiseLike<Result>), params?: RetryActionParams): Promise<Result>;
}
interface RetryActionParams {
  /**
   * If the function returns a pending promise, and it is not fulfilled after this amount of milliseconds, – fail the attempt
   */
  promiseTimeout?: number;

  /**
   * Value for `retry.after(...)`
   *
   * Note:
   * This being `undefined` (or omitted) will produce recursive retries (i.e., call stack overflow is possible)
   * To "pop" no-delay calls from the call stack, either set this to `0` (to invoke `retry.after(0)`),
   * or set `forceAsync` param to `true` (see below)
   */
  retryDelay?: Delay;

  /**
   * Low-level param that governs how to treat no-delay retries (i.e., if `retryDelay` is not present or is `0`).
   * If this is `true`, then no-delay retries will always invoke `retry.after(0)`,
   * which delegates to `setTimeout` internally, thus making the call asynchronous.
   * If this is `false`, then no particular logic is added (see description of `retryDelay`).
   *
   * Note:
   * If `retryDelay` is set to a non-zero value, this param is ignored (delayed retries are always asynchronous)
   *
   * @default false
   */
  forceAsync?: boolean;

  /**
   * Value for the first argument of `retry.setMaxCount(...)`
   */
  maxCount?: number;

  /**
   * Value for the second argument of `retry.setMaxCount(...)`
   */
  onMaxCountExceeded?: OnMaxRetryCountExceeded;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Change: minor[Issue / PR] describes a non-breaking change, such as adding a new functionalityDomain: main[Issue / PR] describes change in the functionality, its optimizationPending: blocked[Issue / PR] cannot be addressed until another issue is resolvedPending: unclear[Issue] not yet fully definedPriority: medium[Issue / PR] should be addressed without delayType: improvement[Issue / PR] addresses lack of a functionality or an open possibility of enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions