Platform: Asynchronous meta reducer support

Created on 25 Mar 2019  路  8Comments  路  Source: ngrx/platform

Meta reducers seemingly don't support async operations. Therefore, I have to create multiple actions to overcome this limitation. You should be able to perform an async operation within a meta reducer that will allow, for example, the payload to be adjusted based on the response of an HTTP request. Such an approach would remove the boilerplate required to have multiple actions and effects to handle this. Please see https://stackoverflow.com/questions/55319705/ngrx-asynchronous-meta-reducer for an example of the problem I am facing.

Describe any alternatives/workarounds you're currently using

The workaround I am using is described in the Stack Overflow issue.

Other information:

Redux supports using promises as action payload's and resolving those promises within middleware before passing the result to the reducers. See libs such as redux-promise and redux-thunk - Can ngrx please support this? It will remove the need for a lot of boilerplate.

If accepted, I would be willing to submit a PR for this feature

[ ] Yes (Assistance is provided if you need help submitting a pull request)
[x ] No

I don't think I have the technical know how to implement this.

Most helpful comment

I wanted to use an async meta reducer to save/restore state from Ionic storage, (ie SQLite) and these operations are async.

All 8 comments

As the answer stated, Effects is what you use for async tasks. Reducers and Meta-Reducers are synchronous

Why has this been closed? This is a feature request. No consideration has been given as to whether this feature will be implemented or not. It has just been closed.

In reference to you comment ... "As the answer stated" ... this is a workaround and not an answer, you will notice that the answer was provided by me to my own question.

There are benefits of meta reducers being asynchronous. For example, in the scenario given in the Stack Overflow issue, use of an asynchronous meta reducer would remove the boilerplate of having a separate loaded action. Redux and other store based libs support asynchronous operations in meta reducers (AKA middleware). In addition, they support promises as payloads which can be resolved within meta reducers. Please reopen this and, at the very least, provide some consideration as to whether this feature can be implemented. Thanks.

I wanted to use an async meta reducer to save/restore state from Ionic storage, (ie SQLite) and these operations are async.

This is the biggest failing of the otherwise superb ngrx lib IMO and is the reason why I will be seeking an alternative in future projects.

@pjc2007 as @brandonroberts pointed out you can use ngrx Effects to achieve this but it adds a lot of boilerplate.

@danday74 yes I spent most of the day removing the meta reducer once I realised it can't possibly work with async calls to a database.

I put the loading of the states via an effect in response to an action to "hydrate". The saving I put in a select rather than an effect, as I want to save it out after the reducers have run, and the word was not to dispatch actions from the reducers.

A bit asymmetric, but I think it is going to work..

@pjc2007

once I realised it can't possibly work with async calls to a database

I bet it's possible but you have to do it yourself.

@danday74

With a factory you can get rid of most of the boilerplate code. And you can made it in a way that the creator of the action doesn't have to know the url.

Was this page helpful?
0 / 5 - 0 ratings