Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove abort event listner for AbortController #3951

Merged
merged 2 commits into from
Jan 19, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 9 additions & 4 deletions packages/toolkit/src/createAsyncThunk.ts
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,14 @@ export const createAsyncThunk = /* @__PURE__ */ (() => {
}
}

const abortedPromise = new Promise<never>((_, reject) =>
abortController.signal.addEventListener('abort', () =>
const abortedPromise = new Promise<never>((_, reject) => {
abortHandler = () => {
EskiMojo14 marked this conversation as resolved.
Show resolved Hide resolved
reject({
name: 'AbortError',
message: abortReason || 'Aborted',
})
)
)
}
EskiMojo14 marked this conversation as resolved.
Show resolved Hide resolved
})
dispatch(
pending(
requestId,
Expand Down Expand Up @@ -653,6 +654,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