How to implement cancellation of ongoing API calls in redux-saga

0 votes
Can i know How to implement cancellation of ongoing API calls in redux-saga?
4 days ago in Node-js by Ashutosh
• 22,830 points
35 views

1 answer to this question.

0 votes

To cancel ongoing API calls in redux-saga, use takeLatest, race, cancel, and fork.

1. Using takeLatest (Auto-cancel previous):

function* fetchDataSaga() {

  yield call(apiCall);

}

function* watchFetch() {

  yield takeLatest('FETCH_REQUEST', fetchDataSaga);

}

2. Manual Cancel with fork + cancel:

import { fork, cancel, take, call } from 'redux-saga/effects';

function* fetchDataSaga() {

  yield call(apiCall);

}

function* watchFetchWithCancel() {

  while (true) {

    yield take('FETCH_REQUEST');

    const task = yield fork(fetchDataSaga);

    const action = yield take(['CANCEL_FETCH', 'FETCH_SUCCESS', 'FETCH_FAILURE']);

    if (action.type === 'CANCEL_FETCH') {

      yield cancel(task);  // Cancel API call

    }

  }

}

3. Using race (Cancel on timeout or other event):

import { race, call, put } from 'redux-saga/effects';

function* fetchDataSaga() {

  const { response, cancelled } = yield race({

    response: call(apiCall),

    cancelled: take('CANCEL_FETCH'),

  });

  if (cancelled) {

    yield put({ type: 'FETCH_CANCELLED' });

  } else {

    yield put({ type: 'FETCH_SUCCESS', payload: response });

  }

}

answered 44 minutes ago by anonymous

Related Questions In Node-js

0 votes
0 answers

How to use the call effect in redux-saga for API requests?

With the help of python programming How ...READ MORE

6 days ago in Node-js by Ashutosh
• 22,830 points
36 views
0 votes
0 answers

How to manage async API calls in a React app using redux-thunk?

Can you tell me How to manage ...READ MORE

5 days ago in Node-js by Ashutosh
• 22,830 points
23 views
0 votes
0 answers

How to integrate soap xml API of unicommerce in nodejs

I am trying to hit xml Api ...READ MORE

Aug 11, 2022 in Node-js by Neha
• 9,020 points
732 views
0 votes
0 answers

How to integrate soap xml API of unicommerce in nodejs?

I'm attempting to use the Node.js soap ...READ MORE

Aug 11, 2022 in Node-js by Neha
• 9,020 points
661 views
0 votes
1 answer

How to differentiate between takeLatest and takeEvery in redux-saga?

Feature takeEvery takeLatest Execution Behavior Executes every triggered action. Executes only the ...READ MORE

answered 1 hour ago in Node-js by anonymous
27 views
0 votes
1 answer

How to integrate redux-saga middleware into a React project?

To integrate redux-saga middleware into a React ...READ MORE

answered 1 hour ago in Node-js by anonymous
27 views
0 votes
1 answer

How to handle async form submissions in React using redux-saga?

To handle async form submissions in React ...READ MORE

answered 1 hour ago in Node-js by anonymous
29 views
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP