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.

Redux-Saga can help ajax request-response squence issue?

See original GitHub issue

Hello 😃

Recently i met AJAX response sequence problem for dispatching single property in redux single store.

Each dispatching process work with below action sequence.

1. Change browser location by history API and Router.onEnter invoke action creator (react-router)

2. 'FETCH_OBJECTS_REQUEST'    // dispatching before request ajax. (no data update)

3. axios.get('/api/file/{objectId}')   // fetching data

4. 'FETCH_OBJECTS_SUCCESS'    // update single store

4-1. 'FETCH_OBJECTS_FAIL'    // just show errors

The problem is. API response time depend to ‘objectId’ parameters.

For example. each /file/{objectId} takes time to /file/A -> 1s, /file/B -> 5s.

User access to /file/A page. and move to /file/B by click some links and go back by browsers ‘back’ button immediatly.

In this case.

API request order

0--------------1--------------2--------------3--------------4--------------5
[/file/A]  [/file/B]  [/file/A]

API response order will be

0--------------1--------------2--------------3--------------4--------------5
               [/file/A]        [/file/A]                          [/file/B]

Browser location will be /file/A. but result of /file/B reducer. store data fill with B object data.

So i plan to validate each response by ‘currentObjectId’ and ‘lastAPIToken’ before reducing them. For example, after third request sent. the state will be currentObjectId: 'A', lastAPIToken: '3'. then every reducer validate response data whether matched with flag state before dispatch. so response of first /file/A and last /file/B will be ignored.

Instead of make upper validation. Is Redux-Saga can help this situations? as i expected, takeLatest accept second response?

Issue Analytics

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

github_iconTop GitHub Comments

6reactions
kuycommented, Jun 5, 2016

@minhyeong-kim Yes, you don’t need to write validation logic because the previously spawned task is always cancelled by takeLatest function internally.

redux-saga-takelatest

  1. [A] Task A is spawned by FETCH_OBJECTS_REQUEST action
  2. [A'] Receive AJAX result and Task A was finished successfully
  3. [B] Task B is spawned (there is no running tasks at this time)
  4. [C] Before Task C is spawned, Task B (waiting AJAX response) is cancelled. Then Task C is started.
1reaction
yelouaficommented, Jun 6, 2016

@kuy Appreciate your help (and other contributions). Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

redux saga ajax call - not working as expected - Stack Overflow
The problem is that after the return res in firstApiRequest , I wanted to use the data to send the FIRST_REQUEST_DONE action ,...
Read more >
API Reference - Redux-Saga
Creates an Effect description that instructs the middleware to wait for a specified action on the Store. The Generator is suspended until an...
Read more >
redux-saga-requests - npm
Redux -Saga addon to simplify handling of AJAX requests. It supports Axios, Fetch API and GraphQL, but different integrations could be added, ...
Read more >
Understanding Redux Saga: From action creators to sagas
Learn how to use Redux Saga for both simple and complex approaches to implementing async operations with action creators, thunks, and sagas.
Read more >
Chapter 6. Handling complex side effects - Redux in Action
As we've alluded, sagas aren't the answer to every problem. ... a saga needs to do the processing of your side effect, such...
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