Comments (3)
If you want to automatically dispatch then you need to set the dispatchReturn option to true, set a success action type too and remove dispatch and done from the process arguments.
from redux-logic.
Thanks for responding @cesarp.
Yes, @ak99372 when using the original callback mode (with process(deps, dispatch, done)
signature) then successType and failType decorate things that are dispatched, so dispatch(obj)
would be decorated by successType if it was defined and dispatch(err)
would be decorated by failType if it was specified. You still call done() when you are finished.
The newer async auto dispatch mode (using the process(deps)
signature or by setting dispatchReturn to true) makes the result dispatched with the result being decorated by successType or failType depending on whether it was an error. This works with sync returns as well as returning a promise or observable. A reject would use the failType decorator if specified. You can also use async process(deps)
signature to take advantage of async/await. Throwing from within an async function will be caught and rejected, so failType would also pick it up.
So you can switch into this mode by omitting the extra arguments (dispatch and done) or by specifying dispatchReturn option to true. I usually just omit the arguments so it is clear I don't need to do anything with them.
You code would look like this
const myLogic= createLogic({
type: "FETCH",
processOptions: {
failType: "FETCH_ERROR"
},
process({ getState }) {
const promise = fnThatReturnsPromise();
return promise;
}
});
OR using async/await style
const myLogic= createLogic({
type: "FETCH",
processOptions: {
failType: "FETCH_ERROR"
},
async process({ getState }) {
const result = await fnThatReturnsPromise();
return result;
}
});
And if you had thrown this would properly be decorated by failType
const myLogic= createLogic({
type: "FETCH",
processOptions: {
failType: "FETCH_ERROR"
},
async process({ getState }) {
throw new Error('myerror'); // will be decorated by failType
}
});
from redux-logic.
Thank you both for reply and indeed adding dispatchReturn
to options now dispatches the failType action. Few related comments:
- it's not very obvious that these two are even related and (without understanding how things work under the hood) I would expect exception inside process to trigger fail action (regardless whether return is dispatched or not). Maybe there is other reason (than implementation)?
- other way around is it even possible (in my original example) for the
failType
to be triggered? (and if not maybe a warning in console when this option is set but can't occur would be helpful) - I read about the (3) modes in docs but the fact that (exception handling) behavior is different based on number of arguments is just trouble waiting to happen. We have a lot of logic that uses dispatch but as requirements change over the time it is possible that the same logic no longer needs dispatch (or to dispatch return) but I don't think any developer expects exception to be handled differently (when optional arguments are removed) especially when options is where people focus on to set the behavior.
Hope that helps with some usability feedback.
from redux-logic.
Related Issues (20)
- hook for optimistic ui updates
- Error in StandardAction generic type definition ? HOT 7
- Type error when specifying payload type HOT 2
- Why is CreateLogic.Config.Transform.Hook uses optional argument for reject? HOT 2
- How to delay a dispatch HOT 2
- done cb in finally does not trigger dispatch HOT 2
- Can not make 'latest' property work HOT 2
- processReturn plus processMultiple
- Feature request: `logicMiddleWare.removeLogic(logics)` HOT 2
- can't test Timers and/or cancelType HOT 1
- Is this repo archived, can't see any active development HOT 2
- Type Error while trying to dispatch an Observable
- Improve bundle size by dropping RxJS and improving build process?
- example of post using ajax HOT 4
- `NODE_ENV` is always '' (empty string) in browser. HOT 1
- createLogic type can be string array
- Websockets redux-logic and SSR
- redux-logic v4.0.0 is not in npmjs.com HOT 1
- action (with type) in `catchError` returned observable is not dispatched HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from redux-logic.