0 added
0 removed
Original
2026-01-01
Modified
2026-02-21
1
<p>One of the most complicated tasks in building front-end applications is dealing with external requests. Difficulties come from two sides.</p>
1
<p>One of the most complicated tasks in building front-end applications is dealing with external requests. Difficulties come from two sides.</p>
2
<p>On the one hand, asynchrony itself generates ambiguities, so the standard mechanisms stop working. Redux doesn't know how to work asynchronously, so all the processing of requests takes place outside Redux. In this case, any non-trivial logic for processing asynchronous actions will appear inside React components:</p>
2
<p>On the one hand, asynchrony itself generates ambiguities, so the standard mechanisms stop working. Redux doesn't know how to work asynchronously, so all the processing of requests takes place outside Redux. In this case, any non-trivial logic for processing asynchronous actions will appear inside React components:</p>
3
<p>On the other hand, the network is an unreliable thing. Requests can take a long time or fail, so we should monitor them to ensure we get the correct response. If the request takes a long time, we show a spinner. If the request is interrupted, we display the corresponding warning:</p>
3
<p>On the other hand, the network is an unreliable thing. Requests can take a long time or fail, so we should monitor them to ensure we get the correct response. If the request takes a long time, we show a spinner. If the request is interrupted, we display the corresponding warning:</p>
4
<p>We write a lot of similar code, even for a few calls. In real applications, the number of calls could be dozens or hundreds. Therefore, you can't do without a ready-made solution. To automate HTTP requests, we need two mechanisms:</p>
4
<p>We write a lot of similar code, even for a few calls. In real applications, the number of calls could be dozens or hundreds. Therefore, you can't do without a ready-made solution. To automate HTTP requests, we need two mechanisms:</p>
5
<ul><li>The<em>redux-thunk</em>middleware, already included in Redux Toolkit</li>
5
<ul><li>The<em>redux-thunk</em>middleware, already included in Redux Toolkit</li>
6
<li>The createAsyncThunk() mechanism</li>
6
<li>The createAsyncThunk() mechanism</li>
7
</ul><p>The<em>redux-thunk</em>middleware allows asynchronous code within dispatch(). That way, we can bring the logic of queries and storage updates into separate functions called<strong>thunks</strong>:</p>
7
</ul><p>The<em>redux-thunk</em>middleware allows asynchronous code within dispatch(). That way, we can bring the logic of queries and storage updates into separate functions called<strong>thunks</strong>:</p>
8
<p>You can do roughly the same thing without<em>redux-thunk</em>just by writing an asynchronous function to which we pass dispatch() as input. The difference shows up in the more advanced use cases. For example, when we work with state or global objects, such as a WebSocket connection. In this case, you have to use<em>redux-thunk</em>, which makes this all much easier to do:</p>
8
<p>You can do roughly the same thing without<em>redux-thunk</em>just by writing an asynchronous function to which we pass dispatch() as input. The difference shows up in the more advanced use cases. For example, when we work with state or global objects, such as a WebSocket connection. In this case, you have to use<em>redux-thunk</em>, which makes this all much easier to do:</p>
9
<p>Despite the convenience gained, thunks do not reduce the amount of code, and the same error handling will make up a large part of the code. This is where createAsyncThunk():</p>
9
<p>Despite the convenience gained, thunks do not reduce the amount of code, and the same error handling will make up a large part of the code. This is where createAsyncThunk():</p>
10
<p>Each thunk created with createAsyncThunk() contains three reducers:<em>pending</em>,<em>fulfilled</em>, and<em>rejected</em>. They correspond to promise states and are called by Redux Toolkit the moment the promise enters one of these states. We don't have to respond to all, so we can choose what is important to us in the application.</p>
10
<p>Each thunk created with createAsyncThunk() contains three reducers:<em>pending</em>,<em>fulfilled</em>, and<em>rejected</em>. They correspond to promise states and are called by Redux Toolkit the moment the promise enters one of these states. We don't have to respond to all, so we can choose what is important to us in the application.</p>