Skip to content

Commit 074d82e

Browse files
committed
per review comments
1 parent b6fbefc commit 074d82e

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

src/createAsyncThunk.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,12 @@ describe('createAsyncThunk with abortController', () => {
160160
thunkAction.abort('AbortReason')
161161
const result = await promise
162162
const expectedAbortedAction = {
163-
type: 'test/aborted',
163+
type: 'test/rejected',
164164
error: {
165165
message: 'AbortReason',
166166
name: 'AbortError'
167167
},
168-
meta: { reason: 'AbortReason' }
168+
meta: { aborted: true, abortReason: 'AbortReason' }
169169
}
170170
// abortedAction with reason is dispatched after test/pending is dispatched
171171
expect(store.getState()).toMatchObject([
@@ -198,12 +198,12 @@ describe('createAsyncThunk with abortController', () => {
198198
const result = await store.dispatch(thunkAction)
199199

200200
const expectedAbortedAction = {
201-
type: 'test/aborted',
201+
type: 'test/rejected',
202202
error: {
203203
message: 'AbortReason',
204204
name: 'AbortError'
205205
},
206-
meta: { reason: 'AbortReason' }
206+
meta: { aborted: true, abortReason: 'AbortReason' }
207207
}
208208
// abortedAction with reason is dispatched without test/pending being dispatched
209209
expect(store.getState()).toMatchObject([{}, expectedAbortedAction])

src/createAsyncThunk.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,16 @@ export function createAsyncThunk<
8383
}
8484
)
8585

86-
const aborted = createAction(
87-
type + '/aborted',
88-
(requestId: string, args: ActionParams, abortError: DOMException) => {
89-
return {
90-
payload: undefined,
91-
meta: { args, requestId, reason: abortError.message },
92-
error: miniSerializeError(abortError)
93-
}
94-
}
95-
)
96-
9786
const rejected = createAction(
9887
type + '/rejected',
9988
(error: Error, requestId: string, args: ActionParams) => {
89+
if (error.name === 'AbortError') {
90+
return {
91+
payload: undefined,
92+
error: miniSerializeError(error),
93+
meta: { args, requestId, aborted: true, abortReason: error.message }
94+
}
95+
}
10096
return {
10197
payload: undefined,
10298
error: miniSerializeError(error),
@@ -110,7 +106,7 @@ export function createAsyncThunk<
110106

111107
let dispatchAbort:
112108
| undefined
113-
| ((reason: string) => ReturnType<typeof aborted>)
109+
| ((reason: string) => ReturnType<typeof rejected>)
114110

115111
let abortReason: string
116112

@@ -129,12 +125,12 @@ export function createAsyncThunk<
129125
extra: TA['extra']
130126
) {
131127
const requestId = nanoid()
132-
let abortAction: ReturnType<typeof aborted> | undefined
128+
let abortAction: ReturnType<typeof rejected> | undefined
133129
dispatchAbort = reason => {
134-
abortAction = aborted(
130+
abortAction = rejected(
131+
new DOMException(reason, 'AbortError'),
135132
requestId,
136-
args,
137-
new DOMException(reason, 'AbortError')
133+
args
138134
)
139135
dispatch(abortAction)
140136
return abortAction

0 commit comments

Comments
 (0)