0

On the snippet of code below I am having a hard time understanding how result = next(action) doesn't end up being an infinite loop. Since the "last function" action => takes parameter action and calculates the result value based on the function that takes the next argument (next => action =>) that then call the function that takes the action parameter (next => action =>) AGAIN.

Essentially the recursiveness of this code is hard to understand.

import C from '../constants'
import appReducer from './reducers'
import { createStore, applyMiddleware } from 'redux'

const consoleMessages = store => next => action => {

    let result

    console.groupCollapsed(`dispatching action => ${action.type}`)
    console.log('ski days', store.getState().allSkiDays.length)
    result = next(action)

    let { allSkiDays, goal, errors, resortNames } = store.getState()

    console.log(`

        ski days: ${allSkiDays.length}
        goal: ${goal}
        fetching: ${resortNames.fetching}
        suggestions: ${resortNames.suggestions}
        errors: ${errors.length}

    `)

    console.groupEnd()

    return result

}

export default (initialState={}) => {
    return applyMiddleware(consoleMessages)(createStore)(appReducer, initialState)

1 Answer 1

2

next is not the name of one of your functions, it's the parameter name.

Calling next doesn't call the action => {...} function.

There isn't any recursion here.

Rewriting the code may clarify things for you:

function consoleMessages(store) {
  return function (next) {
    return function (action) {
      ...
      let result = next(action)
      ...
    }
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. So when an "action" happens then it triggers the entire thing. what is the next "object" then doing when you give it the action? is the console message trying to display the action that is happening now ?
is the reason to add the action => {..} function is having the ability to do an asynchronous function? Since you could argue "result = next (action)" could be implemented in a function that accepts both the next & action object. Not sure why the last function is necessary.
"what is the next 'object' then doing when you give it the action?" next is a function. I don't know what it does. Feel free to look it up in the applyMiddleware API.
"is the reason to add the action => {..} function is having the ability to do an asynchronous function?" I don't exactly understand what you're trying to ask here. I think what you're not understanding is that the applyMiddleware function requires a parameter that behaves in a certain way. The reason for the behavior is entirely on the applyMiddleware function, so my recommendation is that you go review the source if you want to understand how it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.