Lifecycle Actions in createAsyncThunk
When you create an async thunk using createAsyncThunk in Redux Toolkit, it automatically generates three distinct action types that represent the lifecycle of an asynchronous operation — pending, fulfilled, and rejected.
1. pending: Dispatched when the async function is first called. Typically used to set a loading state or show a spinner in the UI.
2. fulfilled: Dispatched when the async operation completes successfully. The returned data becomes the action’s payload.
3. rejected: Dispatched when the async operation fails due to an error, timeout, or rejection. Contains the error message for debugging or display.
These lifecycle actions allow developers to handle loading, success, and error states in a predictable and structured way, reducing boilerplate and improving async flow management.