Skip to content

Commit a05ed0c

Browse files
authored
Merge pull request #2383 from josh-degraw/docs
2 parents 9c4e157 + 3830399 commit a05ed0c

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

docs/api/createAsyncThunk.mdx

+30
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,36 @@ const fetchUserById = createAsyncThunk(
507507
)
508508
```
509509

510+
### Checking if a Promise Rejection was from an Error or Cancellation
511+
512+
To investigate behavior around thunk cancellation, you can inspect various properties on the `meta` object of the dispatched action.
513+
If a thunk was cancelled, the result of the promise will be a `rejected` action (regardless of whether that action was actually dispatched to the store).
514+
515+
- If it was cancelled before execution, `meta.condition` will be true.
516+
- If it was aborted while running, `meta.aborted` will be true.
517+
- If neither of those is true, the thunk was not cancelled, it was simply rejected, either by a Promise rejection or `rejectWithValue`.
518+
- If the thunk was not rejected, both `meta.aborted` and `meta.condition` will be `undefined`.
519+
520+
So if you wanted to test that a thunk was cancelled before executing, you can do the following:
521+
522+
```ts no-transpile
523+
import { createAsyncThunk, isRejected } from '@reduxjs/toolkit'
524+
525+
test('this thunk should always be skipped', async () => {
526+
const thunk = createAsyncThunk(
527+
'users/fetchById',
528+
async () => throw new Error('This promise should never be entered'),
529+
{
530+
condition: () => false,
531+
}
532+
)
533+
const result = await thunk()(dispatch, getState, null)
534+
535+
expect(result.meta.condition).toBe(true)
536+
expect(result.meta.aborted).toBe(false)
537+
})
538+
```
539+
510540
## Examples
511541

512542
- Requesting a user by ID, with loading state, and only one request at a time:

0 commit comments

Comments
 (0)