Saltar al contenido principal

Function: useRace()

useRace<T>(promise, timeoutMs, errorMessage?): Promise<T>

Defined in: src/core/services/timing.service.ts:372

Races a promise against a timeout. Clears the timer when the promise wins so the timeout rejection cannot become an unhandled rejection.

Type Parameters

T

T

The promise's resolved type.

Parameters

promise

Promise<T>

The promise to race.

timeoutMs

number

Timeout in milliseconds.

errorMessage?

string = "Operation timed out"

Error message on timeout (defaults to "Operation timed out").

Returns

Promise<T>

The resolved value of the promise.

Throws

If the timeout fires first.

Example

try {
const data = await useRace(fetch("/api"), 5000);
} catch (e) {
console.error(e); // "Operation timed out"
}