Blog>
Snippets

Handling Parallel API Calls with Redux-Saga

Illustrate the use of the `all` effect in Redux-Saga to perform multiple API calls in parallel and wait for all of them to finish.
import { all, call } from 'redux-saga/effects';

function* fetchUser() {
  const user = yield call(Api.fetchUser);
  return user;
}

function* fetchProjects() {
  const projects = yield call(Api.fetchProjects);
  return projects;
}
Here we define two generator functions, fetchUser and fetchProjects, each making a separate API call using the call effect from redux-saga.
import { all } from 'redux-saga/effects';

function* fetchUserAndProjects() {
  const [user, projects] = yield all([
    call(fetchUser),
    call(fetchProjects)
  ]);
  // handle the response for user and projects
}
This snippet shows how to use the all effect to run both fetchUser and fetchProjects sagas in parallel. The destructuring assignment gets the results from both API calls, allowing you to handle them as needed.