How to respond to reduction-related middleware sends the received action to the send?

I am trying to learn about middleware for promises using redux docs reaction, but don't understand then part below:

 const vanillaPromise = store => next => action => { if (typeof action.then !== 'function') { return next(action) } return Promise.resolve(action).then(store.dispatch) } 

How then knows what to send? The action was not passed as an argument of type

 return Promise.resolve(action).then(function (action) {store.dispatch(action}) 

so I don’t understand how sending gets the action.

+6
source share
1 answer

Hope I can help with this explanation.

allows you to see what you are familiar with:

 return Promise.resolve(action) .then(function (action) { store.dispatch(action)} ) 

You see this part:

 function (action) { store.dispatch(action)} 

This is just a function waiting for the action property to be passed.

Now, when we look at the fact that you are having problems related to your brain, here's what:

 return Promise.resolve(action) .then(store.dispatch) // <--- this part 

"submit" is just a function, in which case an argument is expected. Most likely, the object is like this:

 store.dispatch({ type: 'MY_ACTION_TYPE' })} 

now you can β€œwrap” it in closure, like this, and it will look familiar:

 .then( (action) => { store.dispatch(action) }) 

but do we really need to β€œwrap” it with an anonymous function? In fact, we can just put: store.dispatch, and this is a wait function to pass an argument from the return of the promise. think of it this way:

  var MultiplyByTwo = (x) => x * 2 Promise.resolve(5).then(MultiplyByTwo) // see here, this function is gonna be called with the resolution with the 5 

when we look at the "MultipleByTwo" function - you have a familiar signature that you know about: (x) => x * 2

If we just delete the function name, then the same thing:

 Promise.resolve(5).then((x) => x * 2) 

Note. You see that the solution (5) β†’ thinks about this solution. Then as a chain or β€œhandover”. When we β€œsolve (5),” we pass this value β€œ5” forward to β€œ. Then.” Now remember that the value 5 can be any ... primitive, 5 in this case, the {TYPE: "WHATEVER"} object, function, etc. It just comes off. For example, "Hey, here is my value ...."

 resolve(5).then(myfunction) | ^ |__>__>__>__>__>_| 

It is important to understand that "myFunction" is the example above, either in our example multiplyByTwo or even in the store.dispatch example. they ALL expect passing arguments.

 multiplyByTwo(x) <-- expecting one argument 

or your function may not declare it inside the function signature, but it will be inside the body, ala ..

 myFunction() { const args = Array.from(arguments) // we grab the arguments } 

or hoping for any number of arguments

 myOtherFunction(...args) 

But yes - these functions are expected when any input from the permission will act. There may be times when you don't care about the return value, if there is one, you just want to have some flow control ... do it, "THEN", that ..

I hope this was helpful, and I hope that I really answered your question. If not, let me know.

+4
source

All Articles