question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sequence of promises

See original GitHub issue

First 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:closed
  • Created 6 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
AVVScommented, May 28, 2017

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 😃

0reactions
AVVScommented, Aug 4, 2018

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found