Sequence of promises
See original GitHub issueFirst of all, thanks for good work. I was struggling much when tried to set this library up for universal rendering (documentation requires little improvements 😅 and maybe I’ll help you with that later).
A little question:
module.exports = asyncConnect(
[{
key: 'products',
promise: ({params, store: {dispatch, getState}}) => {
return dispatch(fetchProducts())
.then(data => {
const someValues = reduce(data.products, (values, product) => {
if (product.products.length) {
...
}
return values;
}, []);
let size = 300,
filter,
value = find(someValues, v => v.label === 'String');
filter = `neededValues.id=in=(${value})`;
return dispatch(fetchSomeOtherData({
size,
filter
}));
});
}
],
mapStateToProps, {
...
}
)(Component);
Guess the picture is clear. I need to fetch data, after that fetch another data. Could you tell me please, is this code correct or may be there’s another, right way? And why params (which is route params?) are empty? 😞
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Resolve promises one after another (i.e. in sequence)?
Sequential running of promises is not usually a very good way to use promises. It's usually better to use Promise.all, and let the...
Read more >How to execute multiple promises sequentially in JavaScript
In this approach, we will use Promise.allSettled() which will be executed in a quite similar manner as Promise.all() method executed by taking ...
Read more >Promises chaining - The Modern JavaScript Tutorial
A Promise keeps an internal state. It calls the handler wrapped in a try...catch block and stores the error. That is why asynchronous...
Read more >How to Sequentially Resolve an Array of Promises in JavaScript
Sometimes you need to process an array of Promises in sequence. Here is how to do it in JavaScript with a for loop....
Read more >How to Resolve JavaScript Promises Sequentially (one by one)
Resolving Promises sequentially in Javascript using Arrays, loops and or reduce is pretty easy. Here's how to do that.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
If you nest components then sequencing would be performed automatically, otherwise you solution is perfectly fine.
as for params - I believe it could be changed in the react-router, all the props are merely what’s been passed to a component from the router, so you might want to check the router object. I dont remember the exact structure from the memory, you can always console.log on the opts in the promise to see what you get 😃
track promises manually, your nested dispatches should return some sort of promises or thunks or whatever, which you can keep track of in the asyncConnect. Otherwise it has no knowledge of whats happening inside and cant really tell whether you are done loading or not. Ideally anything that triggers an async action and needs to be resolved - must be connected somehow to the asyncConnect