Search⌘ K
AI Features

Using Promise Lifecycle Actions From createAsyncThunk

Explore how createAsyncThunk in Redux Toolkit leverages promise lifecycle actions to simplify asynchronous state management. Understand how to use pending, fulfilled, and rejected actions within extraReducers to handle loading, success, and error states without manual action creation.

We'll cover the following...

Introduction

To abstract the async state patterns commonly used in thunks, createAsyncThunk makes use of the standard Promise object’s state.

Here’s a reminder. A Javascript promise can only be in one of these states:

  • Pending: Initial state that is neither fulfilled nor rejected.
  • Fulfilled: The operation was completed successfully.
  • Rejected: The operation failed.

As you may have figured out, pending could be likened to the loading state, fulfilled to the success state, and rejected could be likened to the error state.

How does this help?

Here’s an example. Let’s say you created an async thunk via createAsyncThunk. For example:

createAsyncThunk('action-type', async (params, thunkAPI) => {
   //
...