API reference / @evolu/common / Task / timeout
Function: timeout()
function timeout<T, E>(duration, task): Task<T, TimeoutError | E>;
Defined in: packages/common/src/Task.ts:387
Adds timeout behavior to a Task.
Example
interface FetchError {
readonly type: "FetchError";
readonly error: unknown;
}
// Task version of fetch with proper error handling and cancellation support.
const fetch = (url: string) =>
toTask((context) =>
tryAsync(
() => globalThis.fetch(url, { signal: context?.signal ?? null }),
(error): FetchError => ({ type: "FetchError", error }),
),
);
// `satisfies` shows the expected type signature.
fetch satisfies (url: string) => Task<Response, FetchError>;
const fetchWithTimeout = (url: string) => timeout("2m", fetch(url));
const result1 = await fetchWithTimeout("https://api.example.com/data")();
result1 satisfies Result<Response, FetchError | TimeoutError>;
// With AbortController
const controller = new AbortController();
const result2 = await fetchWithTimeout("https://api.example.com/data")(
controller,
);
result2 satisfies Result<Response, FetchError | TimeoutError | AbortError>;
Type Parameters
| Type Parameter |
|---|
T |
E |
Parameters
| Parameter | Type |
|---|---|
duration | Duration |
task | Task<T, E> |
Returns
Task<T,
| TimeoutError
| E>