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.

How to chain multiple data source loads that depends on each other?

See original GitHub issue

Hi,

Lets say I want to to make sure data source A, B and C are loaded to the redux store. Data source B is dependent on A and must be loaded after that A is loaded and data source C is dependent on data source B and must be loaded after B is loaded.

I also want to do this in a single function so I can do it in the same function scope.

How do I do this in a nice way in redux saga?

My take on this is the following:

...
export function* loadMultipleDataSources() {
    // load data source A
    yield put(document_actions.myDocumentsLoad());
    yield take(
    [
        document_actions.MY_DOCUMENTS_LOAD_CACHED,
        document_actions.MY_DOCUMENTS_LOAD_SUCCEEDED,
    ]
    );

    // load data source B, this must happend after 'MY_DOCUMENTS' are in the redux store
    // load data source C...

export function* myDocumentsLoad() {
    const my_documents = yield select(state => state.documents.get('all'));
    if (my_documents.size > 0) {
        yield put({
            type: document_actions.MY_DOCUMENTS_LOAD_CACHED,
        });
        return;
    }
    ...

However, this does not work because of how javascript works. The MY_DCOCUMENTS_LOAD_CACHED will trigger before the code in (1) has come to its take.

However, if i tweak (2) a bit, by adding a delay it works.

yield call(delay, 0); // needs to be here for take() to finish setup
yield put({
    type: document_actions.MY_DOCUMENTS_LOAD_CACHED,
});

Now, the take in (1) will finish up before the put in (2).

Is this a good way to solve the problem? Is there some better solution to this problem?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
Andaristcommented, Sep 20, 2016

Ok. This might be in fact a little bit confusing - the ordering of things here. I would say the easiest and the most ‘clean’ way of handling this is using channel. You can do smth like that:

export function* loadMultipleDataSources() {
    const chan = yield actionChannel([
        document_actions.MY_DOCUMENTS_LOAD_CACHED,
        document_actions.MY_DOCUMENTS_LOAD_SUCCEEDED,
    ]);
    // load data source A
    yield put(document_actions.myDocumentsLoad());
    yield take(chan);

    // load data source B, this must happend after 'MY_DOCUMENTS' are in the redux store
    // load data source C...

It will buffer those actions internally in the created channel, so you wont miss them.

0reactions
neurosnapcommented, Sep 20, 2016

Do the actions you dispatch run sagas? If so then they aren’t all inside one function. It’s up to you how you want to organize your application, but one of the big benefits to sagas are their composability.

Read more comments on GitHub >

github_iconTop Results From Across the Web

load two DS into same DSO - SAP Community
DSO Allowed you to load data from multiple Data Sources.Its activation is totally depends on the volume of data request contain. Make sure...
Read more >
Promises chaining - The Modern JavaScript Tutorial
They don't pass the result to each other; instead they process it ... In practice we rarely need multiple handlers for one promise....
Read more >
Troubleshoot Data Blending - Tableau Help
The primary and secondary connections are from tables in the same data source. Instead of linking the connections, use the Data menu to...
Read more >
Concurrent function in Power Apps - Microsoft Learn
Evaluates multiple formulas concurrently with one another. ... use Concurrent to improve performance when the app loads data.
Read more >
How to make a dependent (cascading) drop-down list in Excel
To put it differently, we will make an Excel data validation list based on the value of another list. Creating a multiple dependent...
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