Skip to content

Commit 2a7505e

Browse files
author
João Dias
committed
docs(make-cancelable): updated documentation
1 parent b50a55b commit 2a7505e

File tree

1 file changed

+57
-4
lines changed

1 file changed

+57
-4
lines changed

docs/docs/functions/utils/make-cancelable.mdx

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: makeCancelable
33
---
4-
Wraps a native Promise and allows it to be cancelled using AbortController. This is useful for cancelling long-running operations or preventing memory leaks when a component unmounts before an async operation completes.
4+
Wraps a native Promise and allows it to be cancelled using AbortController. This is useful for cancelling long-running operations or preventing memory leaks when a component unmounts before an async operation completes. The function also provides access to the underlying AbortSignal, which can be used to coordinate cancellation across multiple promises or network requests.
55

66
## API
77

@@ -11,14 +11,24 @@ interface MakeCancelablePromise<T = unknown> {
1111
* The wrapped promise that can be aborted
1212
*/
1313
promise: Promise<T>;
14+
1415
/**
1516
* Aborts the promise execution. Safe to call multiple times - subsequent calls will be ignored if already cancelled.
17+
* @param reason - Optional reason for the cancellation
1618
*/
17-
cancel: () => void;
19+
cancel: (reason?: any) => void;
20+
1821
/**
1922
* Checks whether the promise has been cancelled
2023
*/
2124
isCancelled: () => boolean;
25+
26+
/**
27+
* The AbortSignal object that can be used to check if the promise has been cancelled.
28+
* This signal can be used to coordinate cancellation across multiple promises or network requests
29+
* by passing it to other abortable operations that should be cancelled together.
30+
*/
31+
signal: AbortSignal;
2232
}
2333

2434
function makeCancelable<T = unknown>(promise: Promise<T>): MakeCancelablePromise<T>;
@@ -53,6 +63,15 @@ cancelable.cancel();
5363
if (cancelable.isCancelled()) {
5464
console.log('Promise was already cancelled');
5565
}
66+
67+
// Use the signal with other abortable operations
68+
fetch('/api/data', { signal: cancelable.signal })
69+
.then(response => response.json())
70+
.catch(error => {
71+
if (error instanceof AbortPromiseError) {
72+
console.log('Fetch was cancelled');
73+
}
74+
});
5675
```
5776

5877
### React Example
@@ -65,8 +84,16 @@ function MyComponent() {
6584
useEffect(() => {
6685
const cancelable = makeCancelable(fetchData());
6786

68-
cancelable.promise
69-
.then(setData)
87+
// Use the signal with multiple operations
88+
const fetchUser = fetch('/api/user', { signal: cancelable.signal });
89+
const fetchSettings = fetch('/api/settings', { signal: cancelable.signal });
90+
91+
Promise.all([cancelable.promise, fetchUser, fetchSettings])
92+
.then(([data, user, settings]) => {
93+
setData(data);
94+
setUser(user);
95+
setSettings(settings);
96+
})
7097
.catch(error => {
7198
if (error instanceof AbortPromiseError) {
7299
// Handle cancellation
@@ -105,3 +132,29 @@ try {
105132
}
106133
}
107134
```
135+
136+
### Coordinating Multiple Operations
137+
138+
The `signal` property can be used to coordinate cancellation across multiple operations. This is particularly useful when you need to cancel multiple related operations together:
139+
140+
```typescript
141+
const cancelable = makeCancelable(fetchData());
142+
143+
// Use the same signal for multiple operations
144+
const operation1 = new Promise((resolve, reject) => {
145+
cancelable.signal.addEventListener('abort', () => {
146+
reject(new AbortPromiseError());
147+
});
148+
// ... operation logic
149+
});
150+
151+
const operation2 = new Promise((resolve, reject) => {
152+
cancelable.signal.addEventListener('abort', () => {
153+
reject(new AbortPromiseError());
154+
});
155+
// ... operation logic
156+
});
157+
158+
// Cancelling the original promise will also cancel all operations using its signal
159+
cancelable.cancel();
160+
```

0 commit comments

Comments
 (0)