Skip to content

Commit d50f92b

Browse files
authored
Merge pull request #3951 from Fonger/abort-cancel-subscription
Remove abort event listner for AbortController
2 parents 72f867e + 8f278e8 commit d50f92b

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

packages/toolkit/src/createAsyncThunk.ts

+10-4
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,15 @@ 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+
abortController.signal.addEventListener('abort', abortHandler)
612+
})
611613
dispatch(
612614
pending(
613615
requestId,
@@ -653,6 +655,10 @@ export const createAsyncThunk = /* @__PURE__ */ (() => {
653655
err instanceof RejectWithValue
654656
? rejected(null, requestId, arg, err.payload, err.meta)
655657
: rejected(err as any, requestId, arg)
658+
} finally {
659+
if (abortHandler) {
660+
abortController.signal.removeEventListener('abort', abortHandler)
661+
}
656662
}
657663
// We dispatch the result action _after_ the catch, to avoid having any errors
658664
// here get swallowed by the try/catch block,

0 commit comments

Comments
 (0)