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

async TypedMiddleware? #96

Closed
MihaMarkic opened this issue Jan 15, 2019 · 4 comments
Closed

async TypedMiddleware? #96

MihaMarkic opened this issue Jan 15, 2019 · 4 comments

Comments

@MihaMarkic
Copy link

It'd be nice to have async versions of TypedMiddleware.

return (Store<AppState> store, LoginResultAction action, Future<NextDispatcher> next) async {
    await next(action);
    ....

So the next dispatcher can be awaited.
Does that make sense?

@brianegan
Copy link
Owner

Hey there -- sorry, thought I'd put in an answer to that Q!

Could you describe the use-case a bit more to see if we can find a good solution?

Overall, I definitely understand why this would be helpful, but this would essentially break the fundamental promise that Redux gives you: Actions are dispatched and reduced synchronously.

This means you can do store.dispatch on one line, and the very next line store.state will be in the expected state given the update. If we made next async, this means any action in the chain could delay the reducer from running synchronously, and I think that will lead to a lot more headaches.

@MihaMarkic
Copy link
Author

I see. I was thinking about what if you have to execute an async operation within middleware, such as if you want to persist the current state and the persistence method is asynchronous? Or perhaps you need some other async method to proceed? Isn't this something that middleware should support? Or not?

@brianegan
Copy link
Owner

brianegan commented Mar 5, 2019

Isn't this something that middleware should support? Or not?

It certainly is! The main trick: You can't use async/await, but you can still do myFuture.then(...).then(...) or listen to Streams just fine.

That said, if you'd like to use async/await or Streams in middleware, lot of times I'll use the redux_epics package. It's kind of like Stream-based middleware, and it supports async* functions which allow you to run async/await calls.

https://pub.dartlang.org/packages/redux_epics

@MihaMarkic
Copy link
Author

Basically calling async methods without waiting, I guess it makes sense.

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