Skip to content

Commit

Permalink
Merge pull request #3951 from Fonger/abort-cancel-subscription
Browse files Browse the repository at this point in the history
Remove abort event listner for AbortController
  • Loading branch information
EskiMojo14 committed Jan 19, 2024
2 parents 72f867e + 8f278e8 commit d50f92b
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions packages/toolkit/src/createAsyncThunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,7 @@ export const createAsyncThunk = /* @__PURE__ */ (() => {
: nanoid()

const abortController = new AbortController()
let abortHandler: (() => void) | undefined
let abortReason: string | undefined

function abort(reason?: string) {
Expand All @@ -600,14 +601,15 @@ export const createAsyncThunk = /* @__PURE__ */ (() => {
}
}

const abortedPromise = new Promise<never>((_, reject) =>
abortController.signal.addEventListener('abort', () =>
const abortedPromise = new Promise<never>((_, reject) => {
abortHandler = () => {
reject({
name: 'AbortError',
message: abortReason || 'Aborted',
})
)
)
}
abortController.signal.addEventListener('abort', abortHandler)
})
dispatch(
pending(
requestId,
Expand Down Expand Up @@ -653,6 +655,10 @@ export const createAsyncThunk = /* @__PURE__ */ (() => {
err instanceof RejectWithValue
? rejected(null, requestId, arg, err.payload, err.meta)
: rejected(err as any, requestId, arg)
} finally {
if (abortHandler) {
abortController.signal.removeEventListener('abort', abortHandler)
}
}
// We dispatch the result action _after_ the catch, to avoid having any errors
// here get swallowed by the try/catch block,
Expand Down

0 comments on commit d50f92b

Please sign in to comment.