W
W
Worddoc2019-05-30 17:16:42
JavaScript
Worddoc, 2019-05-30 17:16:42

Redux-saga: how to call callback after executing multiple Ajax requests from different sagas?

Greetings!
Stack: react, redux, redux-saga.
Problem: there is a preloader and a bunch of ajax requests that are distributed over different sagas. 1 request controls 1 saga. How can I wait for each of these requests to complete and call the callback?
Purpose: hide the preloader only after successful or unsuccessful execution of each request.
I really hope for help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Suntsev, 2019-05-30
@Worddoc

Solved a similar problem.
There is an option.
The problem is that requests are asynchronous and we cannot predict which ones will be slower and which ones will be faster.
An option to solve this problem is to create a pair of Ashns, BEGIN_FETCHING and END_FETCHING.
Add an object to the store that would accumulate the download process, by default it is 0.
At the beginning of the request, we do

yield put({
      type: BEGIN_FETCHING
    })

and at the end
yield put({
      type: END_FETCHING
    })

in the reducer, we prescribe that BEGIN_FETCHING increases the value by 1, and END_FETCHING decreases it,
and when our object value becomes 0, we remove the preloader.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question