Skip to content

Commit dc930f9

Browse files
author
João Dias
committed
fix(make-cancelable): removed isCancelled and added abort reasons
1 parent 5704855 commit dc930f9

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/functions/utilities/make-cancelable.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ export interface MakeCancelablePromise<T = unknown> {
2323
* The wrapped promise that can be aborted
2424
*/
2525
promise: Promise<T>;
26+
2627
/**
2728
* Aborts the promise execution. Safe to call multiple times - subsequent calls will be ignored if already cancelled.
2829
*/
29-
cancel: () => void;
30+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
31+
cancel: (reason?: any) => void;
32+
3033
/**
3134
* Checks whether the promise has been cancelled
32-
* @returns {boolean} True if the promise has been cancelled, false otherwise
3335
*/
3436
isCancelled: () => boolean;
3537
}
@@ -94,7 +96,6 @@ export interface MakeCancelablePromise<T = unknown> {
9496
*/
9597
export function makeCancelable<T = unknown>(promise: Promise<T>): MakeCancelablePromise<T> {
9698
const controller = new AbortController();
97-
let isCancelled = false;
9899

99100
const wrappedPromise = new Promise<T>((resolve, reject) => {
100101
// Early return if already cancelled
@@ -105,7 +106,6 @@ export function makeCancelable<T = unknown>(promise: Promise<T>): MakeCancelable
105106

106107
// Add abort signal listener
107108
const abortHandler = () => {
108-
isCancelled = true;
109109
reject(new AbortPromiseError());
110110
};
111111

@@ -133,13 +133,14 @@ export function makeCancelable<T = unknown>(promise: Promise<T>): MakeCancelable
133133

134134
return {
135135
promise: wrappedPromise,
136-
cancel() {
137-
if (!isCancelled) {
138-
controller.abort();
136+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
137+
cancel(reason?: any) {
138+
if (!controller.signal.aborted) {
139+
controller.abort(reason);
139140
}
140141
},
141142
isCancelled() {
142-
return isCancelled;
143+
return controller.signal.aborted;
143144
},
144145
};
145146
}

0 commit comments

Comments
 (0)