Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Saborter Changelog

## v2.0.0
## v2.0.1 (March 1th, 2026)

### Bug Fixes

- Fixed argument injection in `setTimeoutAsync` and `debounce` functions [#54](https://github.com/TENSIILE/saborter/pull/54)

## v2.0.0 (February 24th, 2026)

### Breaking Changes

Expand Down
26 changes: 10 additions & 16 deletions docs/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ import { AbortError } from 'saborter/errors';
- **Type:** `string`
- **Description:** The default stack field is 'Error' without extended information. To enable extended information, [see here](#🎯-usage-examples).

`metadata?`

- **Type:** `any`
- **Description:** Interrupt-related data. The best way to pass any data inside the error. This field will not be overridden in any way.
- **Optional:** `true`

`initiator`

- **Type:** `'timeout' | 'user' | 'system'`
Expand All @@ -76,7 +82,7 @@ new AbortError(message, options?)
- `options?: Object`
- `type?: 'cancelled' | 'aborted'` (Default is `aborted`) - Abort type.
- `reason?: any` - Additional reason for interruption.
- `signal?: AbortSignal` - AbortSignal that was just interrupted.
- `metadata?: any` - Interrupt-related data. The best way to pass any data inside the error.

## 🎯 Usage Examples

Expand All @@ -89,28 +95,16 @@ console.error(error.message); // 'The operation was interrupted'
console.error(error.type); // 'aborted'
```

### Creation with type and reason
### Creation with type and metadata

```javascript
const error = new AbortError('Request cancelled', {
type: 'cancelled',
reason: { requestId: '123', userId: 'user_456' }
metadata: { requestId: '123', userId: 'user_456' }
});

console.error(error.type); // 'cancelled'
console.error(error.reason); // { requestId: '123', userId: 'user_456' }
```

### Creation with signal

```javascript
const aborter = new Aborter(); // You can also use AbortController

const error = new AbortError('The operation was interrupted', {
signal: aborter.signal
});

console.log(error.signal); // The signal that was transmitted
console.error(error.metadata); // { requestId: '123', userId: 'user_456' }
```

### Active additional debug information in the error stack
Expand Down
23 changes: 13 additions & 10 deletions docs/libs.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,11 @@ try {

### `setTimeoutAsync`

Schedules the execution of a handler (either a function or a string of code) after a specified delay. The operation can be cancelled using an `AbortSignal`. This function returns a promise that resolves with the handler's result or rejects if the timeout is aborted or if the handler throws an error.
Schedules the execution of a handler after a specified delay. The operation can be cancelled using an `AbortSignal`. This function returns a promise that resolves with the handler's result or rejects if the timeout is aborted or if the handler throws an error.

**Parameters:**

- `handler: string | ((signal: AbortSignal) => T | Promise<T>)`: Can be either:
- A string of code to evaluate (similar to the first argument of `setTimeout`).
- `handler: ((signal: AbortSignal) => T | Promise<T>)`:
- A function that accepts an `AbortSignal` and returns a value or a `Promise`. This function will be called with the signal to allow cleanup on abort.
- `delay?: number` - The time in milliseconds to wait before executing the handler. If omitted, the handler is scheduled without a delay (i.e., as soon as possible).
- `options?: Object`:
Expand All @@ -232,11 +231,15 @@ Schedules the execution of a handler (either a function or a string of code) aft

```typescript
const controller = new AbortController();
setTimeoutAsync((signal) => {
return fetch('/api/data', { signal }).then((res) => res.json();
}), 5000, {
signal: controller.signal
})
setTimeoutAsync(
(signal) => {
return fetch('/api/data', { signal }).then((res) => res.json());
},
5000,
{
signal: controller.signal
}
)
.then((data) => console.log(data))
.catch((error) => console.log(error.name)); // 'AbortError' if aborted
```
Expand All @@ -248,7 +251,7 @@ const controller = new AbortController();
try {
const data = await setTimeoutAsync(
async (signal) => {
const response = fetch('/api/data', { signal });
const response = await fetch('/api/data', { signal });
return await response.json();
},
5000,
Expand All @@ -262,7 +265,7 @@ try {
#### The `setTimeoutAsync` function can be used as a delay function if you need it:

```typescript
const delay = (ms: number) => setTimeoutAsync('', ms);
const delay = (ms: number) => setTimeoutAsync(() => null, ms);

console.log('Hello');
await delay(2000);
Expand Down
Loading