0

I'm trying to build a middleware in redux to handle api requests.

While looking for inspiration I found this code: redux api middleware

export default store => next => action => {
  const callAsync = action[CALL_API];

  if(typeof callAsync === 'undefined') {
    return next(action);
  }

  .
  .
  .

  function actionWith(data) {
    const finalAction = assign({}, action, data);
    delete finalAction[CALL_API];

    return finalAction;
  }

  next(actionWith({ type: types.REQUEST }));
  .
  .
  .
}

My question is: Why is the function actionWith decalared inside of the main function? Wouldn't it be more simple if the function was decalared outside and one will pass the function the action object also?

What is the benefit here?

2
  • 1
    In JavaScript, functions are objects. Having a nested function provides a child context (scope) for the nested function to operate within, much like class members in other languages. Commented Apr 13, 2018 at 18:16
  • 1
    This should give you some more info : stackoverflow.com/questions/7295634/javascript-nested-function Commented Apr 13, 2018 at 18:19

1 Answer 1

2

Wouldn't it be more simple if the function was decalared outside and one will pass the function the action object also?

You are correct: you could have take actionWith outside of the outer function as long as you supplied action as an argument (actionWith(data, action)).

The functionality would be the same. However, the primary concern I have is maintainability: if you needed to modify the inner function to do something that required yet another variable from the outer function , you'd need to add another argument. If the duty of the function is closely tied to the internals of the outer function, leaving it in gives you ready access to the outer function's variables when you need to modify the code.

I would balance this concern of extra arguments (which generally favors keeping the function internal) against the usefulness of having the function available to other part of the code (which would favor taking it outside, for increased visibility). For example, if I had many outer functions that each had their own internal copies of actionWith, it would be better to have them to share a single version of actionWith.

That is, if I had

function outer1(action) {
    function actionWith(data) { ... }
    actionWith(thing);
}

function outer2(action) {
    function actionWith(data) { ... }
    actionWith(thing);
}

From a maintainability perspective, I would rather have

function actionWith(action, data) { ... }

function outer1(action) {
    actionWith(action, thing);
}

function outer2(action) {
    actionWith(action, thing);
}
Sign up to request clarification or add additional context in comments.

Comments

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.