How to test timeout on redux saga

Viewed 179

I am new in react-native and redux saga. I would like to cancel may request if the timeout is already 0.

Saga

...

const cancelSource = axios.CancelToken.source();

try {
  const { response, timeout } = yield race({
    posts: call(api.callFunction, cancelSource.token),
    timeout: delay(0),
  })

  console.log(response); // return success

  if (timeout) {
    console.log('TIMEOUT', timeout)
    yield cancel();
  }

  ...
} 
catch () {
 if (yield cancelled()) {
   yield call(cancelSource.cancel)
 }
}
finally {
 if (yield cancelled()) {
   yield call(cancelSource.cancel)
 }
}

api

    callFunction(cancelToken) {
        return axiosClient
            .post(config.baseURL + '/something', {}, {
                cancelToken: cancelToken
            })
            .then((responseJson) => {
                return responseJson;
            })
            .catch((error) => {
                return error;
            });
    },

but, the result of timeout was true since the delay was set to 0. But the response is also success. Therefore the api call was success even though the timeout is already 0.

Am I doing right? or is there another way to cancel the api call if the timeout is already done?.

PS

I also tried the setTimeout then cancel the saga.

Thanks!

0 Answers
Related