Skip to content

Commit 7d8115b

Browse files
committed
Remove abort event listner for AbortController
1 parent 5eb3680 commit 7d8115b

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

packages/toolkit/src/createAsyncThunk.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,7 @@ export const createAsyncThunk = /* @__PURE__ */ (() => {
577577
: nanoid()
578578

579579
const abortController = new AbortController()
580+
let abortHandler: (() => void) | undefined
580581
let abortReason: string | undefined
581582

582583
function abort(reason?: string) {
@@ -600,14 +601,14 @@ export const createAsyncThunk = /* @__PURE__ */ (() => {
600601
}
601602
}
602603

603-
const abortedPromise = new Promise<never>((_, reject) =>
604-
abortController.signal.addEventListener('abort', () =>
604+
const abortedPromise = new Promise<never>((_, reject) => {
605+
abortHandler = () => {
605606
reject({
606607
name: 'AbortError',
607608
message: abortReason || 'Aborted',
608609
})
609-
)
610-
)
610+
}
611+
})
611612
dispatch(
612613
pending(
613614
requestId,
@@ -653,6 +654,10 @@ export const createAsyncThunk = /* @__PURE__ */ (() => {
653654
err instanceof RejectWithValue
654655
? rejected(null, requestId, arg, err.payload, err.meta)
655656
: rejected(err as any, requestId, arg)
657+
} finally {
658+
if (abortHandler) {
659+
abortController.signal.removeEventListener('abort', abortHandler)
660+
}
656661
}
657662
// We dispatch the result action _after_ the catch, to avoid having any errors
658663
// here get swallowed by the try/catch block,

0 commit comments

Comments
 (0)