Skip to content

Commit 0cd6708

Browse files
committed
support 3rd-party thenables, handle async errors same way as sync
1 parent 0b8fabb commit 0cd6708

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

packages/toolkit/src/createAsyncThunk.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -554,12 +554,11 @@ If you want to use the AbortController to react to \`abort\` events, please cons
554554
let finalAction: ReturnType<typeof fulfilled | typeof rejected>
555555
try {
556556
let conditionResult = options?.condition?.(arg, { getState, extra })
557-
if (conditionResult instanceof Promise) {
558-
try {
559-
conditionResult = await conditionResult
560-
} catch {
561-
conditionResult = false
562-
}
557+
if (
558+
typeof conditionResult === 'object' &&
559+
typeof conditionResult.then === 'function'
560+
) {
561+
conditionResult = await conditionResult
563562
}
564563
if (conditionResult === false) {
565564
// eslint-disable-next-line no-throw-literal

packages/toolkit/src/tests/createAsyncThunk.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,10 @@ describe('conditional skipping of asyncThunks', () => {
615615
const condition = () => Promise.reject()
616616
const asyncThunk = createAsyncThunk('test', payloadCreator, { condition })
617617
await asyncThunk(arg)(dispatch, getState, extra)
618-
expect(dispatch).toHaveBeenCalledTimes(0)
618+
expect(dispatch).toHaveBeenCalledTimes(1)
619+
expect(dispatch).toHaveBeenLastCalledWith(
620+
expect.objectContaining({ type: 'test/rejected' })
621+
)
619622
})
620623

621624
test('rejected action is not dispatched by default', async () => {

0 commit comments

Comments
 (0)