Skip to content

Commit ed1d21e

Browse files
committed
Merge branch 'master' of https://github.com/reduxjs/redux-toolkit into fix-composeWithDevTools-spy
2 parents 3a5074c + 3c50985 commit ed1d21e

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

docs/api/createAsyncThunk.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ If a thunk was cancelled, the result of the promise will be a `rejected` action
527527
So if you wanted to test that a thunk was cancelled before executing, you can do the following:
528528

529529
```ts no-transpile
530-
import { createAsyncThunk, isRejected } from '@reduxjs/toolkit'
530+
import { createAsyncThunk } from '@reduxjs/toolkit'
531531

532532
test('this thunk should always be skipped', async () => {
533533
const thunk = createAsyncThunk(

examples/query/react/kitchen-sink/src/features/auth/authSlice.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const slice = createSlice({
2424
console.log('fulfilled', action)
2525
state.user = action.payload.user
2626
state.token = action.payload.token
27+
state.isAuthenticated = true
2728
})
2829
.addMatcher(postsApi.endpoints.login.matchRejected, (state, action) => {
2930
console.log('rejected', action)

packages/toolkit/src/createAsyncThunk.ts

Lines changed: 10 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,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)