See FrankerZ's comment, apparently this dispatch
function is a standard Redux thing (I don't use Redux) and it doesn't return a promise, so it doesn't make sense to use Promise.all
on it.
But answering the promises aspect of "how would I wait for getThis
first": Just move it to the beginning of the chain:
return function (dispatch, getState) {
dispatch(getThis()) // First this, then...
.then(result => Promise.all([ // ...these
dispatch(getThat(result)), // (I've passed the result to all three, but...)
dispatch(getThese(result)),
dispatch(getThose(result))
]))
.then(() => {
let state = getState();
dispatch({
//dispatch
});
}).catch(function (err) {
console.log(err);
});
}
or without the dispatch
, since apparently that's wrong:
return function (dispatch, getState) {
getThis() // First this, then...
.then(result => Promise.all([ // ...these
getThat(result), // (I've passed the result to all three, but...)
getThese(result),
getThose(result)
]))
.then(() => {
let state = getState();
dispatch({
//dispatch
});
}).catch(function (err) {
console.log(err);
});
}
Side note about getThis
: See this question and its answers. No need for new Promise
when you already have a promise. I don't know whether that getThis
code is correct in terms of Redux, but here's the same code not using new Promise
unnecessarily:
export function getThis() {
return function(dispatch, getState) {
return axios.get('api/this/' + lienBuild).then(response => dispatch({type: "FETCH_THIS", data: response.data}));
};
}
or with destructuring and shorthand properties:
export function getThis() {
return function(dispatch, getState) {
return axios.get('api/this/' + lienBuild).then(({data}) => dispatch({type: "FETCH_THIS", data}));
};
}
or if you can use async
/await
:
export function getThis() {
return async function(dispatch, getState) {
const {data} = await axios.get('api/this/' + lienBuild);
return dispatch({type: "FETCH_THIS", data});
};
}