How to set timeout for yield call in React Redux-Saga
Asked Answered
S

3

7

In my rest API backend I do heavy processing and usually, it takes 1.5 minutes to produce a result, in that time I'm getting this error in my frontend react application.

Error: timeout of 60000ms exceeded

So, peer connection is lost.

How do I set request timeout in redux-saga

Sweat answered 8/3, 2019 at 10:26 Comment(1)
This could be happening for a number of reasons and you haven't given any detail on how your app is set up. Have you checked your webserver/proxy for timeout limits? Are there timeout limits on the frontend ajax calls being made by react?Remonstrate
D
10

i was used race for such things. May be it will useful for you.

  const {posts, timeout} = yield race({
    posts: call(fetchApi, '/posts'),
    timeout: delay(60 * 1000)
  });
  if (timeout) throw new Error('timeout of 60000ms exceeded') 
Drawee answered 10/8, 2020 at 16:8 Comment(0)
G
1
import { eventChannel, END } from 'redux-saga'

function countdown(secs) {
  return eventChannel(emitter => {
      const iv = setInterval(() => {
        secs -= 1
        if (secs > 0) {
          emitter(secs)
        } else {
          // this causes the channel to close
          emitter(END)
        }
      }, 1000);
      // The subscriber must return an unsubscribe function
      return () => {
        clearInterval(iv)
      }
    }
  )
}

Hope this helps.

Ginglymus answered 8/3, 2019 at 12:55 Comment(0)
S
1
export function* create(action) {
  try {
    const { payload } = action;
    const response = yield call(api.addPost, payload);
    if (response.status === 200) {
      console.log('pass 200 check');
      yield put(appActions.setResourceResponse(response.data));
      console.log(response.data);
      payload.push('/add-news');
    }
  } catch (error) {
    console.log(error);
    yield put(
      a.setResponse({
        message: error.response.data,
        status: error.response.status,
      }),
    );
  }
}
Sweat answered 8/3, 2019 at 14:38 Comment(1)
the timeout occurs during the API call from django rest APISweat

© 2022 - 2024 — McMap. All rights reserved.