Skip to main content

Function: raceWithAbort()

function raceWithAbort<T>(
operation: Promise<T>,
signal: AbortSignal
): Promise<T>;

Defined in: shared/utils/abortUtils.ts:678

Races a promise against an AbortSignal.

Type Parametersโ€‹

Tโ€‹

T

Resolved value of the underlying operation.

Parametersโ€‹

operationโ€‹

Promise<T>

Promise to monitor.

signalโ€‹

AbortSignal

Abort signal to race against.

Returnsโ€‹

Promise<T>

The first settled result between the promise and the abort event.

Remarksโ€‹

Useful for APIs without native abort support. The returned promise rejects as soon as the signal aborts, allowing callers to adopt a uniform cancellation flow.

Exampleโ€‹

const controller = new AbortController();

const result = await raceWithAbort(
someNonAbortableOperation(),
controller.signal
);

Throwsโ€‹

Error when the signal aborts before the operation settles.