Blog>
Snippets

Running Tasks Concurrently with all Effect

Provide an example of how to run multiple tasks concurrently in a saga using the `all` effect, which waits for all tasks to complete.
import { all, call } from 'redux-saga/effects';

function* fetchUserDetails() {
  // Simulate fetching user details
}

function* fetchUserTransactions() {
  // Simulate fetching user transactions
}

function* fetchInitialData() {
  yield all([
    call(fetchUserDetails),
    call(fetchUserTransactions)
  ]);
}
This code defines three generator functions, two of which simulate fetching user details and transactions. The fetchInitialData function then uses the all effect to run these two fetch operations in parallel, using the call effect to invoke them. This is useful when you need to wait for multiple concurrent tasks to complete before continuing.