K
K
Kotik Meow2020-08-04 15:23:11
redux
Kotik Meow, 2020-08-04 15:23:11

How to pause the saga until a certain value is changed in redux?

There are two requests in different sagas, the first request is to get a token, and the second request is authorization. The first request is always for a token. An authorization request can only be made when the response to the first request has arrived. How can you pause the execution of the saga if the answer to the first request has not yet arrived, and the user has already pressed the login button? (Relatively slow Internet). An indicator that the answer to the first request has come is the token hanging in the editor. And after the arrival of the token, continue execution.

Answers to possible questions:
1. There is no need to combine these two sagas into one
2. Requests are always sent in a strictly defined order (token first, then authorization)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
abberati, 2020-08-04
@AlexVais

You have some kind of action, in response to which the reducer puts the token in the store. In the second saga, you can pause it (the saga) before dispatching this action

const actionWhereTokenReceived = yield take(ACTION_TYPE)

The code will stop at this line and resume when the action with the correct type is dispatched. The action itself with the token will fall into the constant.
UPD: If this second saga needs to be called after receiving the token, then it is obvious that it will stop forever (or rather, until a dispatch occurs, which may never happen again). In that case, you can do this:
let token = yield select(state => state.path.to.token)

if (!token) {
  const actionWhereTokenReceived = yield take(ACTION_TYPE)
  token = actionWhereTokenReceived.payload
}

...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question