# freeCodeCamp Challenge Guide: Use Middleware to Handle Asynchronous Actions

**URL:** <https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-use-middleware-to-handle-asynchronous-actions/301451>\
**Category:** freeCodeCamp Contributors\
**Tags:** guide\
**Created:** [August 3, 2019, 1:02pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-use-middleware-to-handle-asynchronous-actions/301451 "2019-08-03T13:02:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![camperbot](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/camperbot/32/18364_2.png) [@camperbot](https://forum.freecodecamp.org/u/camperbot)\
**Post date:** [August 3, 2019, 1:02pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-use-middleware-to-handle-asynchronous-actions/301451/1 "2019-08-03T13:02:59Z")

</div>

# [Use Middleware to Handle Asynchronous Actions](https://www.freecodecamp.org/learn/front-end-libraries/redux/use-middleware-to-handle-asynchronous-actions)

* * *

## Hints

### Hint 1

Treat the `dispatch` argument as a function and pass the action events in it.

### Hint 2

The `requestingData` action event will be passed first.

### Hint 3

The `receivedData` action event will be passed after the `setTimeout` function.

This sequence simulates the process of requesting the data, receiving the data and then dispatching the received data.

### Hint 4

It is important that the `data` variable be passed as an argument of `receivedData`.

* * *

## Solutions

> **Solution 1 (Click to Show/Hide)**
>
> ```javascript
> const REQUESTING_DATA = "REQUESTING_DATA";
> const RECEIVED_DATA = "RECEIVED_DATA";
> 
> const requestingData = () => {
> return { type: REQUESTING_DATA };
> };
> const receivedData = data => {
> return { type: RECEIVED_DATA, users: data.users };
> };
> 
> const handleAsync = () => {
> return function(dispatch) {
> // dispatch request action here
> 
> dispatch(requestingData());
> 
> setTimeout(function() {
> let data = {
> users: ["Jeff", "William", "Alice"]
> };
> // dispatch received data action here
> 
> dispatch(receivedData(data));
> }, 2500);
> };
> };
> 
> const defaultState = {
> fetching: false,
> users: []
> };
> 
> const asyncDataReducer = (state = defaultState, action) => {
> switch (action.type) {
> case REQUESTING_DATA:
> return {
> fetching: true,
> users: []
> };
> case RECEIVED_DATA:
> return {
> fetching: false,
> users: action.users
> };
> default:
> return state;
> }
> };
> 
> const store = Redux.createStore(
> asyncDataReducer,
> Redux.applyMiddleware(ReduxThunk.default)
> );
> 
> ```

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 8, 2025, 3:58pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-use-middleware-to-handle-asynchronous-actions/301451/2 "2025-01-08T15:58:15Z")

</div>

Closed on January 8, 2025, 3:58pm UTC


