Model an async request lifecycle
A request needs standardized loading, success, and failure actions.
Redux Toolkit · Type Alias
AsyncThunk is the thunk action-creator type generated by createAsyncThunk. It coordinates a promise-returning payload creator with pending, fulfilled, and rejected lifecycle actions.
AsyncThunk is the generated thunk type associated with createAsyncThunk. The factory combines an action-type prefix with a payload callback, then produces a callable thunk action creator whose nested pending, fulfilled, and rejected creators describe the promise lifecycle; the payload callback also receives thunkAPI for store access and request controls.
createAsyncThunk<Returned, ThunkArg, ThunkApiConfig>(typePrefix, payloadCreator, options?)| Name | Type | Description |
|---|---|---|
type | string | String prefix used to construct the async lifecycle action types. |
payloadCreator | (arg, thunkAPI) => Promise | value | Callback that receives the thunk argument and thunkAPI and returns a promise or value. |
options | AsyncThunkOptions | Optional configuration for the generated thunk, including behavior such as error handling, cancellation, and conditional execution. |
A standard Redux thunk action creator with nested pending, fulfilled, and rejected action creators; dispatching it returns a promise containing the corresponding lifecycle action object.
import { createAsyncThunk } from '@reduxjs/toolkit'
export const fetchUser = createAsyncThunk(
'users/fetchById',
async (userId: number, thunkAPI) => {
const response = await fetch(`/api/users/${userId}`)
if (!response.ok) throw new Error('Request failed')
return response.json() as Promise<{ id: number; name: string }>
}
)
fetchUser.pending.match({ type: 'users/fetchById/pending' })This creates an async thunk whose attached lifecycle creators can be matched independently.
A request needs standardized loading, success, and failure actions.
A payload callback needs store state, dispatch, cancellation, or request metadata.
A caller must react to a request result after dispatching it.
AsyncThunk represents the thunk action creator produced by createAsyncThunk. It is callable for dispatch and exposes pending, fulfilled, and rejected action creators as nested fields.
Follow-up: How can lifecycle actions be recognized generically? Use matching utilities such as isAsyncThunkAction to type-guard actions created by createAsyncThunk.
The factory takes an action-type prefix and a payload creator, with an optional options object. The payload creator returns a promise, and the thunk dispatches pending before it runs, followed by fulfilled or rejected according to the result.
Follow-up: What context does the payload creator receive? Its second argument is thunkAPI, which includes dispatch, getState, extra, requestId, signal, and rejectWithValue.
Dispatching an AsyncThunk returns a promise that resolves with either the fulfilled action object or the rejected action object. This consistent resolution behavior lets callers inspect the result or use the documented unwrap mechanism.
Follow-up: Why use unwrap? Awaiting dispatch(thunk()).unwrap() makes the caller receive the fulfilled payload or handle a failure at the component level.
createAsyncThunkisAsyncThunkActionpendingfulfilledrejectedunwrap