Zap Studio

Abort Signal

Cancel retry orchestration with AbortSignal before, between, or during retries.

BaseRetryPolicy.run(...) supports AbortSignal through RetryRunOptions.signal.

Use this when retries should stop because of user navigation, request timeout, shutdown, or parent workflow cancellation.

Basic Usage

const controller = new AbortController();

const policy = new ExponentialBackoff({
  maxAttempts: 5,
  baseDelayMs: 100,
  maxDelayMs: 2_000,
});

const promise = policy.run(
  async () => {
    return await doWork();
  },
  { signal: controller.signal },
);

controller.abort(new Error("Request canceled by user"));

await promise;

Throw Mode

In throw mode (default), aborting throws immediately.

import { AbortError } from "@zap-studio/retry/errors";

const controller = new AbortController();

try {
  await policy.run(
    async () => {
      return await doWork();
    },
    { signal: controller.signal },
  );
} catch (error) {
  if (error instanceof AbortError) {
    console.error("Retry aborted:", error.message);
  } else {
    throw error;
  }
}

Non-Throw Mode

With throwOnExhausted: false, abort returns { ok: false } instead of throwing.

const controller = new AbortController();

const result = await policy.run(
  async () => {
    return await doWork();
  },
  {
    signal: controller.signal,
    throwOnExhausted: false,
  },
);

if (!result.ok) {
  console.error(result.error);
} else {
  console.log(result.value);
}

Cancellation Timing

Cancellation is checked:

  • before each attempt starts
  • after a failed attempt
  • while waiting between retries

This keeps cancellation responsive without requiring custom orchestration code.

Edit on GitHub

Last updated on

On this page