--- id: 5a24c314108439a4d4036156 title: Use Middleware to Handle Asynchronous Actions challengeType: 6 isRequired: false videoUrl: '' localeTitle: '' --- ## Description undefined ## Instructions undefined ## Tests
```yml tests: - text: '' testString: 'assert(requestingData().type === REQUESTING_DATA, "The requestingData action creator should return an object of type equal to the value of REQUESTING_DATA.");' - text: '' testString: 'assert(receivedData("data").type === RECEIVED_DATA, "The receivedData action creator should return an object of type equal to the value of RECEIVED_DATA.");' - text: '' testString: 'assert(typeof asyncDataReducer === "function", "asyncDataReducer should be a function.");' - text: '' testString: 'assert((function() { const initialState = store.getState(); store.dispatch(requestingData()); const reqState = store.getState(); return initialState.fetching === false && reqState.fetching === true })(), "Dispatching the requestingData action creator should update the store state property of fetching to true.");' - text: '' testString: 'assert((function() { const noWhiteSpace = handleAsync.toString().replace(/\s/g,""); return noWhiteSpace.includes("dispatch(requestingData())") === true && noWhiteSpace.includes("dispatch(receivedData(data))") === true })(), "Dispatching handleAsync should dispatch the data request action and then dispatch the received data action after a delay.");' ```
## Challenge Seed
```jsx 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 setTimeout(function() { let data = { users: ['Jeff', 'William', 'Alice'] } // dispatch received data action here }, 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) ); ```
## Solution
```js // solution required ```