Wait for State Changes with Redux Saga
Redux Saga is a middleware library for handling side effects in Redux applications. One common challenge is coordinating async operations that depend on a specific piece of state being available before they can proceed — for example, waiting for an authentication token before starting background sync.
This post demonstrates a pattern for detecting when a Redux state value changes and reacting to it within a saga.
The Problem
Imagine a flow where a user signs in, the auth saga stores a loginToken in state, and then a background sync saga needs to start — but only after that token is available. You cannot rely on action sequencing alone because the token may already be in state when the saga starts.
The Pattern
The solution uses a generator function that:
- Immediately checks if the desired state value already exists using
yield select(selector) - If not present, enters a
while (true)loop that listens for every action usingyield take('*') - Re-evaluates the selector after each action until the condition is met
- Returns once the state value becomes available
Implementation
First, define a selector for the state value you are waiting on:
const selectLoginToken = state => state.auth.loginToken;
Next, create a helper saga that waits for the selector to return a truthy value:
function* waitForState(selector) {
let value = yield select(selector);
if (value) return value;
while (true) {
yield take('*');
value = yield select(selector);
if (value) return value;
}
}
Finally, use it in your watcher saga:
function* backgroundSyncSaga() {
yield take('SIGN_IN');
const token = yield call(waitForState, selectLoginToken);
yield put({ type: 'BACKGROUND_SYNC', payload: { token } });
}
export function* rootSaga() {
yield fork(backgroundSyncSaga);
}
How It Works
The waitForState helper is flexible — it works with any selector and any condition. The yield take('*') call blocks until the next action of any type is dispatched, at which point the selector is re-evaluated. This avoids polling and keeps the saga reactive to the Redux action stream.
The initial yield select(selector) check before the loop handles the case where the state is already populated when the saga starts, preventing an unnecessary wait.
Considerations
- Performance: Listening to every action with
take('*')is fine for low-frequency state transitions, but if state updates very rapidly this can become noisy. Consider narrowingtaketo a specific action type if you know which action sets the value. - Infinite loops: If the selector condition can never be met,
waitForStatewill run indefinitely. Add a timeout or cancellation mechanism for production use. - Race conditions: Combine with
raceif you want to abort waiting after a timeout or on an error action.
Conclusion
This pattern provides a clean way to coordinate dependent async operations in Redux Saga without relying on brittle action ordering. By combining select and take('*'), you get a reactive, flexible mechanism for waiting on any state condition before proceeding.