Read a thunk's original result
Use it after dispatching an async thunk when the caller needs the successful data rather than the lifecycle action object.
Redux Toolkit · Function
unwrapResult converts the action returned by a dispatched createAsyncThunk into its original result. It returns a fulfilled payload and throws the rejection payload or error for failed thunks.
unwrapResult accepts the completed action returned by dispatching a createAsyncThunk thunk. It returns the fulfilled action's payload, but throws the rejected action's error or its payload when rejectWithValue supplied one, allowing ordinary promise-style error handling.
unwrapResult(resultAction)| Name | Type | Description |
|---|---|---|
resultAction | AnyAction | The fulfilled or rejected action returned by dispatching a thunk created with createAsyncThunk. |
The fulfilled action payload; throws the rejected action's payload when supplied by rejectWithValue, otherwise throws the rejected action's error.
import { createAsyncThunk, unwrapResult } from '@reduxjs/toolkit'
const fetchUserById = createAsyncThunk('users/fetchById', async (id: string) => {
const response = await fetch(`/api/users/${id}`)
if (!response.ok) throw new Error('Request failed')
return response.json()
})
async function loadUser(dispatch: (action: unknown) => Promise<unknown>) {
try {
const resultAction = await dispatch(fetchUserById('42'))
const user = unwrapResult(resultAction as Parameters<typeof unwrapResult>[0])
console.log(user)
} catch (error) {
console.error(error)
}
}Dispatches an async thunk, converts its completed action into the original payload, and handles either thrown errors or rejectWithValue data.
Use it after dispatching an async thunk when the caller needs the successful data rather than the lifecycle action object.
Place it in a promise chain so rejected thunk outcomes enter catch handling.
Call it inside try/catch when using async/await and custom rejectWithValue payloads.
unwrapResult receives the fulfilled or rejected action produced by dispatching a createAsyncThunk thunk. It returns the fulfilled payload, or throws the rejection error or rejectWithValue payload.
Follow-up: Why might .unwrap() be preferred? The documentation says the promise returned by the dispatched thunk already exposes .unwrap(), and describes using it as preferred in most cases.
A createAsyncThunk dispatch resolves with an action object regardless of whether the request succeeded. Calling unwrapResult converts that action into promise-like behavior by returning the success payload or throwing the failure value.
Follow-up: What happens without unwrapping? The caller receives the fulfilled or rejected action object and must inspect its status or payload itself.
When the thunk calls rejectWithValue with a custom value, a rejected action carries that value as its payload. unwrapResult throws that payload when present; otherwise it throws the serialized error.
Follow-up: How can a caller handle both forms? Put unwrapResult inside try/catch and process the caught value as either the custom rejection payload or the thunk error.
createAsyncThunk.unwrap()rejectWithValue