Comments (14)
Maybe this? (with latest version)
export function createAndStart(name) {
return (dispatch) => {
dispatch({
type: CREATE_AND_START,
payload: {
promise: new Promise(async (resolve, reject) => {
await dispatch(create(name)).payload.promise
await dispatch(start()).payload.promise
resolve(push('/start'))
})
}
});
};
}
Have a look at our tests to get more of an idea of the return from dispatch calls when using a promise-middleware-action
from redux-promise-middleware.
I'm busy for the rest of the week. I'll get back to you this weekend when I have some more time to read through your question to give you a worthwhile answer!
from redux-promise-middleware.
Thanks! Sorry to be a bother, I really want to learn the/a proper way to do things. I'm grateful for the willingness to help.
from redux-promise-middleware.
I was looking for a way to do this type of thing as well. This works, but was wondering if there was a better way:
return ({fetch, settings: {firebaseUrl}}) => {
return ({
type: 'LOAD_PROFILE_USER',
payload: {
promise: fetch(`${firebaseUrl}/users/${slug}.json`)
.then(response => response.json())
.then(response => (users.viewer && users.viewer.slug === response.slug) ?
Promise.resolve(loadProfileSettingsForm(response)) :
Promise.resolve(true))
.catch(err => {
throw err;
}),
}
});
};
from redux-promise-middleware.
Also, if there is some way to do this with aysnc/await?
from redux-promise-middleware.
@tomatau Thank you!! That worked perfectly. I truly appreciate the help, and I'll be sure to dig into the tests to learn more.
@rkoberg Hopefully this example using async/await helps you too...
from redux-promise-middleware.
Sure, but I just moved it to a separate async function (example below). Using Este, it takes care of dispatch for actions:
https://github.com/este/este/blob/master/src/common/configureStore.js#L23
https://github.com/este/este/blob/master/src/common/lib/injectDependencies.js
However, it just uses the dispatch function (not always a promise). What is the benefit of appending .payload.promise
? I want access to the data to transform for the reducer. And I am not clear what you are doing with resolve(push('/start'))
. What is that for?
Here is what I am doing:
async function _login(firebase, fields) {
try {
const authData = await firebase.authWithPassword(fields.toJS());
const userData = await getUserFromAuthData(firebase, authData);
return {authData, userData};
} catch (err) { //eslint-disable-line brace-style
throw err;
}
}
from redux-promise-middleware.
@rkoberg the resolve(push('/start'))
bit was just for my specific example. I needed to redirect upon completion of the second action.
from redux-promise-middleware.
What is the benefit of appending .payload.promise?
The return from dispatch when passed a promise action is an action object with the promise available at payload.promise
, this allows you to await for the promise to be resolved, following @baronnoraz question and code.
I tend to move my async calls into separate functions and avoid action creators depending on each other.
from redux-promise-middleware.
OK. I have setup the firebase functions to return a promise. authWithPassword
resolves into authData
first. Then, async getUserFromAuthData
has an await
inside of it (to another Promisified firebase function). It resolves and all the data is sent in the return to the reducer. In other words, I don't seem to need .payload.promise
-- wondering if I am somehow doing something wrong that currently results in the correct result.
from redux-promise-middleware.
The example I posted was using actionCreator functions inside an actionCreator and awaiting on dispatch calls, check the OP for the API of the functions used in the example code I posted.
from redux-promise-middleware.
@rkoberg I am going to close this since we resolved the OP's question. Would you mind creating a new issue? I'm happy to help.
from redux-promise-middleware.
I think we are good.
from redux-promise-middleware.
One more example:
/*
* redux-thunk + redux-promise-middleware + redux-actions
*/
const _createAndStart = createAction(CREATE_AND_START,
async (name, { dispatch }) => {
await dispatch(create(name));
await dispatch(start());
await dispatch(push('/start'));
});
export const createAndStart = (name) =>
(dispatch) => dispatch(_createAndStart(name, { dispatch }));
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.