Redux - Use Middleware to Handle Asynchronous Actions

Tell us what’s happening:

Instructions were followed to solve this lesson but…

Here’s my present understanding limitation:

Where is ‘handleAsync’ used? Seems nowhere using Ctrl + F search in the script.
using console.log(typeof(handleAsync));, I confirm that this is a function. But it doesn’t seem this is called or run somewhere or supplied as argument to any function.

Do let me know if anyone knows the answer. Thanks in advance!!

Your code so far

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)
);

console.log(typeof(handleAsync));

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36

Challenge Information:

Redux - Use Middleware to Handle Asynchronous Actions

It isn’t really used anywhere.

All the test does is turn the function into a string and search for the two action creators getting dispatched.

https://github.com/freeCodeCamp/freeCodeCamp/blob/main/curriculum/challenges/english/03-front-end-development-libraries/redux/use-middleware-to-handle-asynchronous-actions.md

The Redux docs have some more examples that might be more real-world usage

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.