Skip to content

Commit

Permalink
Remove abort event listner for AbortController
Browse files Browse the repository at this point in the history
  • Loading branch information
Fonger committed Dec 5, 2023
1 parent 5eb3680 commit 50ddabd
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 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,16 @@ 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 +656,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 50ddabd

Please sign in to comment.