Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redux middleware example causes confusion #3858

Closed
jackylong1992 opened this issue Aug 18, 2020 · 5 comments
Closed

Redux middleware example causes confusion #3858

jackylong1992 opened this issue Aug 18, 2020 · 5 comments

Comments

@jackylong1992
Copy link

What docs page needs to be fixed?

One of the seven examples on redux middleware section causes me confused. It is the timeoutScheduler.
https://redux.js.org/advanced/middleware#seven-examples

What is the problem?

I don't think that the cancel() function will ALWAYS be called. The only way we can call this function is register timeoutScheduler as first middleware on the chain then execute store.dispatch()().

What should be changed to fix the problem?

I think we should remove that out of the examples. Since the returned value is NOT important when using middleware.

@markerikson
Copy link
Contributor

The middleware isn't trying to "always" run the cancel function. It's simply generating a form of an "unsubscribe"-type callback, and returning that as the result from dispatch(). That way, the caller can cancel the timer if desired:

const cancelTimer = dispatch({type: 'increment', meta: {delay: 5000}});

// later
cancelTimer();

Returned values are important, because a middleware can return anything it wants from dispatch(). That's how thunk functions are able to return a promise - the thunk middleware returns whatever the thunk function itself returns.

@jackylong1992
Copy link
Author

jackylong1992 commented Aug 19, 2020

@markerikson I don't think you can ALWAYS call to cancelTimer() if you have MORE than one middleware in the chain. The dispatch() only return the returned value from the FIRST middleware in chain.
It will NOT work if we do this:
image

So, how to access the returned value of all seven chained middleware?

@markerikson
Copy link
Contributor

That's how Redux middleware work.

Any middleware may substitute its own return value. If a middleware is written correctly, it should pass later return values forward, ie, return next(action).

However, any middleware in front of that may also substitute its own return value, delay the return value, not return anything, etc.

So, it's a question of how the middleware in the chain are cooperating with each other.

But yeah, this middleware itself is written correctly. Trust me on this :)

@jackylong1992
Copy link
Author

@markerikson thanks for your explanation. It would be better if that flexibility is described more clearly in your document.

@markerikson
Copy link
Contributor

Rewriting the existing "Basics/Advanced" tutorial is literally what I'm working on right now :)

See #3854 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants