Comments (13)
Why isn't it possible with a resolved thunk?
from redux-promise-middleware.
Why isn't it possible with a resolved thunk?
Resolved thunk functionality was removed from the middleware because of the issues caused, e.g., issue #61. Perhaps I am misunderstanding though?
PS: Sorry for the delay. It has been a busy week.
from redux-promise-middleware.
I don't follow. This issue is all about resolving objects but thunks are resolving functions, they shouldn't overlap?
from redux-promise-middleware.
I've made a fork with a branch that keeps the thunk behaviour and also returns the original promise unmodified.
The original error always gets thrown, because it's the original promise. The middleware just creates a new promise internally for creating rejected actions.
Even errors that previously got swallowed (e.g. component errors as the result of an async dispatch) now propagate up.
https://github.com/tomatau/redux-promise-middleware/blob/another-implementation/src/index.js
I've updated the tests on this fork branch too.
from redux-promise-middleware.
Since this is preserved in v2 and you have been maintaining that release, I am going to close this. Maybe you should add a note to the README though (about v2 enabling you to use thunks).
from redux-promise-middleware.
I would really like to see a solution for this in a latest release -- I'm currently getting warnings from version monitoring systems that say I'm out of date with this middleware (and npm outdated
isn't as pretty as it could be) :P
from redux-promise-middleware.
I'm fine with adding it to v3. Do you have time to commit to implementing it in v3?
from redux-promise-middleware.
I think I understand what you want to achieve and I did it this way.
I am using post() and get() from axios package at moment which is promise based.
I added a then(val) handler to this example, so that fulfilled results get an extra conditionFlag
field set.
const OBSERVATION_COMMENT_SAVE = 'OBSERVATION_COMMENT_SAVE';
//
// In this I am capturing the comment.id and making it available on the
// rejected promise result value. This way my reducer has the info.
//
// For my case at least comment may be new and have id === 0.
// My reducer still gets that value.
//
export function saveComment(
comment,
newCommentText,
) {
const { id } = comment;
const url = id
? `${apiUrls.comment}/${id}`
: `${apiUrls.comment}/`;
const promise = post({
url,
data: {
id,
comments: newCommentText
}
})
.then((val) => {
if (myConditionFlag) {
val.conditionFlag = true;
}
return val;
});
.catch((errorVal) => {
if (id) {
errorVal.id = id;
}
return Promise.reject(errorVal);
});
return {
type: OBSERVATION_COMMENT_SAVE,
payload: { promise, data: { id, comments: newCommentText }}
};
}
from redux-promise-middleware.
This doesn't solve the problem of modifying the meta information for _FULFILLED only.
Imagine you have an app that is connected through sockets and you have a middleware that listens for actions. When an action has a meta with { broadcast: true }
you wish to broadcast this action and play it over all other clients connected to the socket. Now - you don't want to play all the pending or rejected actions to everyone as that's sometimes just not very useful... but you do want to broadcast the fulfilled actions!
Thunks solve this problem.
The solution already exists and is working fine in v2.
from redux-promise-middleware.
If you modify .catch(errorVal) in example and use .then(val) and modify val it will modify metadata returned - I modified the example above to include a .then(val) handler to demonstrate.
I think maybe I still don't fully understand the use case you are describing from your most recent comment though, though I think now that any middleware after the action creator should be able to pick up the flag on a FULFILLED state ?
from redux-promise-middleware.
The socket middleware reads meta data, not the action type - this is by design as we want to be explicit about the actions that are broadcast.
Your example modifies the payload, not the meta. Anything resolved by the promise is used to populate the payload.
A Flux Standard Action has the shape:
{
type: string,
?payload: any,
?meta: object,
?error: bool
}
v3 and v4 of redux-promise-middleware take the meta from the original action shape and apply it both the _pending and _fulfilled or _rejected without the capability to modify per type.
from redux-promise-middleware.
You can normally live without thunk resolving feature, see the example: #43 (comment)
from redux-promise-middleware.
@artemjackson not sure I follow, the example you posted is resolving with a name and a thunk!.. :)
from redux-promise-middleware.
Related Issues (20)
- Switch to babel-preset-env HOT 1
- Consider Changing Public API HOT 6
- Question regarding catching error on dispatcher or handling it in reducer with _REJECTED HOT 1
- How to use the returned promise in typescript? HOT 2
- README fix: Upgrading Guide links are broken HOT 1
- Update docs with custom type suffixes page HOT 1
- Applying middleware shows invalid definitions (TS) and causes no actions to be dispatched HOT 3
- Update TypeScript Definitions HOT 1
- Release Version 6 HOT 2
- TypeScript definitions excluded in .npmignore HOT 4
- Fix TypeScript ActionType HOT 2
- Add helpers that add suffixes to action types HOT 2
- I don't know how to transfer other arguments HOT 1
- Upgrade URL (docs.psb.codes) is 404'ing HOT 2
- TypeScript: fulfilled action result from dispatch HOT 11
- Typescript Promise not work with AsyncAction HOT 3
- A question about the middleware's return value HOT 2
- Empty payload when pass nullable payload in getAction HOT 3
- Chaining Actions Typescript
- `promiseTypeDelimiter: ""` is ignored and instead uses default delimiter HOT 4
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-promise-middleware.