Skip to content

Define retry.setMaxCount #76

@parzhitsky

Description

@parzhitsky

Definition:

declare function setMaxCount(retryCount: number, onExceeded: "resolve" | "reject" = "reject"): void;
declare function setMaxCount(retryCount: number, onExceeded: () => unknown): void;

Usage:

(All actions here are limited to 3 attempts total: one initial attempt and two additional retries.)

retry.setMaxCount(2);
retry.setMaxCount(2, "reject");
// rejects with reason: "Maximum retry count exceeded after 2 retries (3 attempts total)"
retry.setMaxCount(2, "resolve");
// resolves `undefined` (a.k.a "silently fails")
retry.setMaxCount(2, () => reject("Could not do this"));
// rejects with reason: "Could not do this"
retry.setMaxCount(2, () => resolve(42));
// resolves `42`

Idempotence

Setting maximum retry count is an idempotent action, — only the first call (per each retry entity) will take effect. This also includes subsequent calls of the same line when retrying the attempt.

retryable((resolve, reject, retry) => {
  retry.setMaxCount(2); // <- first call sets limit to 2 retries / 3 attempts, next calls are ignored
  retry.setMaxCount(5); // <- ignored
  retry.setMaxCount(0); // <- ignored
  retry.setMaxCount(Infinity); // <- ignored
});
retryable((resolve, reject, retry) => {
  retry.setMaxCount(2); // <- first call sets limit to 2 retries / 3 attempts, next calls are ignored
  retry.setMaxCount(NaN); // <- ignored
});
retryable((resolve, reject, retry) => {
  retry.setMaxCount(NaN); // <- throws error (invalid retry count)
  // execution stops, anything after the erroneous line is a dead code
});

One big example with multiple retry entities:

await Promise.all([
  retryable((res, rej, retry) => {
    retry.setMaxCount(17); // <- first call sets limit to 17 retries / 18 attempts, next calls are ignored
    retry.setMaxCount(42); // <- ignored
  }),
  retryable((res, rej, retry) => {
    retry.setMaxCount(42); // <- first call sets limit to 42 retries / 43 attempts, next calls are ignored
    retry.setMaxCount(17); // <- ignored
  }),
]);

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 optimizationPriority: low[Issue / PR] could be addressed at any convenient timeType: improvement[Issue / PR] addresses lack of a functionality or an open possibility of enhancementgood first issue[Issue] can be addressed by a first-time contributor

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions