Blog>
Snippets

Using Spawn for Independent Tasks

Illustrate the creation of a detached task using the `spawn` effect in Redux-Saga, emphasizing its independence in error handling and cancellation.
import { spawn } from 'redux-saga/effects';

function* independentSaga() {
  // Independent logic here
  console.log('This saga runs independently');
}

function* rootSaga() {
  // Using spawn for creating an independent task
  yield spawn(independentSaga);
  console.log('Root saga continues without waiting for independentSaga');
}
This code snippet shows how to use the `spawn` effect to create a detached task. The `independentSaga` is executed independently of the parent saga (`rootSaga`). This means errors in `independentSaga` won't bubble up to `rootSaga`, and cancellation of `rootSaga` won't affect `independentSaga`.