Skip to content

Commit

Permalink
Document timeout function
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiodxa committed Mar 31, 2023
1 parent b95e0f9 commit e5d51f9
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,40 @@ export async function loader({ request }: LoaderArgs) {
}
```

### timeout

The `timeout` function lets you attach a timeout to any promise, if the promise doesn't resolve or reject before the timeout, it will reject with a `TimeoutError`.

```ts
try {
let result = await timeout(fetch("https://example.com"), { ms: 100 });
} catch (error) {
if (error instanceof TimeoutError) {
// Handle timeout
}
}
```

Here the fetch needs to happen in less than 100ms, otherwise it will throw a `TimeoutError`.

If the promise is cancellable with an AbortSignal you can pass the AbortController to the `timeout` function.

```ts
try {
let controller = new AbortController();
let result = await timeout(
fetch("https://example.com", { signal: controller.signal }),
{ ms: 100, controller }
);
} catch (error) {
if (error instanceof TimeoutError) {
// Handle timeout
}
}
```

Here after 100ms, `timeout` will call `controller.abort()` which will mark the `controller.signal` as aborted.

### cacheAssets

> **Note**
Expand Down

0 comments on commit e5d51f9

Please sign in to comment.