apisauce icon indicating copy to clipboard operation
apisauce copied to clipboard

Timeout not working on android.

Open hungdev opened this issue 6 years ago • 25 comments

When app cannot connect to API, the timeout will not be working on android. But ios still run ok. timeout

my package.json

"apisauce": "^0.14.3",
"react-native": "0.54.2",
"react": "16.3.0-alpha.1",
"redux-saga": "^0.16.0"

hungdev avatar Jun 07 '18 02:06 hungdev

Hmm. What android version are we talking here?

skellock avatar Jun 07 '18 11:06 skellock

@skellock I tried with emulator 5.1, 6.1 and 7.

hungdev avatar Jun 07 '18 13:06 hungdev

+1 -- I'm having the same problem.

alfonsosn avatar Sep 25 '18 21:09 alfonsosn

same here , testing on emulator

SalahEddineBC avatar Nov 18 '18 18:11 SalahEddineBC

Hello everyone, for the moment, this is my solution.

import { create, TIMEOUT_ERROR } from 'apisauce';

import apiConfig from './config';

const requestTimeout = (promise, axiosConfig, reqConfig) => {
  const { timeout } = apiConfig;
  const config = { ...axiosConfig, ...(reqConfig || {}) };
  const duration = (parseInt(config.timeout, 10) || timeout) + 1000;
  const timeoutPromise = new Promise(
    (resolve => setTimeout(() => resolve({
      ok: false,
      problem: TIMEOUT_ERROR,
      originalError: 'REQUEST_TIMEOUT_ERROR',
      data: null,
      status: null,
      headers: null,
      config,
      duration
    }), duration))
  );
  return Promise.race([timeoutPromise, promise]);
};

const buildApi = (config = apiConfig) => {
  const api = create(config);
  const {
    axiosInstance: { defaults }, get, delete: del, head, post, put, patch, link, unlink
  } = api;
  api.get = (...args) => requestTimeout(get(...args), defaults, args[2]);
  api.delete = (...args) => requestTimeout(del(...args), defaults, args[2]);
  api.head = (...args) => requestTimeout(head(...args), defaults, args[2]);
  api.post = (...args) => requestTimeout(post(...args), defaults, args[2]);
  api.put = (...args) => requestTimeout(put(...args), defaults, args[2]);
  api.patch = (...args) => requestTimeout(patch(...args), defaults, args[2]);
  api.link = (...args) => requestTimeout(link(...args), defaults, args[2]);
  api.unlink = (...args) => requestTimeout(unlink(...args), defaults, args[2]);
  return api;
};

export default buildApi;

ahce avatar Nov 25 '18 21:11 ahce

Can this be related to https://github.com/facebook/react-native/issues/11666 ?

hnka avatar Jan 11 '19 01:01 hnka

still facing this issue, any updates?

v0lume avatar Apr 05 '19 14:04 v0lume

I am also facing this issue.

MuhammadUsman786786 avatar Apr 25 '19 15:04 MuhammadUsman786786

I am also facing this issue, too.

ThuyDang88 avatar May 03 '19 07:05 ThuyDang88

I am also facing this issue, too [2].

mayermatheus avatar Aug 15 '19 15:08 mayermatheus

same here

gabsbelini avatar Oct 07 '19 20:10 gabsbelini

same here

RodrigoAngeloValentini avatar Oct 25 '19 14:10 RodrigoAngeloValentini

same here

ahmadina avatar Dec 07 '19 12:12 ahmadina

any update on this?

Sajid-Ali avatar Mar 18 '20 09:03 Sajid-Ali

Same here in android emulator. I've deactivated wifi and LTE from the emulator and .get call never finishes, even if i've configured a timeout.

[email protected]
[email protected]

sdandois avatar Sep 18 '20 15:09 sdandois

Still persist in the latest version.

SafdarSikander avatar Jan 03 '21 08:01 SafdarSikander

@SafdarSikander @sdandois @Sajid-Ali is this still an issue ?

absmugz avatar Mar 19 '21 06:03 absmugz

any update on this issue?

RZulfikri avatar May 29 '21 13:05 RZulfikri

Might be related to https://github.com/axios/axios/issues/2073

rcstanciu avatar Aug 02 '21 11:08 rcstanciu

I have used the promise with race conditions to solve this problem

MuhammadUsman786786 avatar Aug 02 '21 11:08 MuhammadUsman786786

the comment from here where @rcstanciu pointed seems to work with the apisauce as well.

basically call a cancelation with a setTimeout() instead of using the built in timeout of apisauce

let axiosCall;
if (Platform.OS === "android") {
    axiosCall = axios.CancelToken.source();
    setTimeout(() => {
        axiosCall.cancel("Timeout of 30000ms exceeded");
    }, 30000);
}
return yourApisauceCreate
    .post("test", payload, {
        ...(axiosCall && {
            cancelToken: axiosCall.token
        })
    })

johhansantana avatar Apr 29 '22 03:04 johhansantana

the comment from here where @rcstanciu pointed seems to work with the apisauce as well.

basically call a cancelation with a setTimeout() instead of using the built in timeout of apisauce

let axiosCall;
if (Platform.OS === "android") {
    axiosCall = axios.CancelToken.source();
    setTimeout(() => {
        axiosCall.cancel("Timeout of 30000ms exceeded");
    }, 30000);
}
return yourApisauceCreate
    .post("test", payload, {
        ...(axiosCall && {
            cancelToken: axiosCall.token
        })
    })

is it working for you .??

eramudeep avatar May 11 '22 04:05 eramudeep

Hello everyone, for the moment, this is my solution.

import { create, TIMEOUT_ERROR } from 'apisauce';

import apiConfig from './config';

const requestTimeout = (promise, axiosConfig, reqConfig) => {
  const { timeout } = apiConfig;
  const config = { ...axiosConfig, ...(reqConfig || {}) };
  const duration = (parseInt(config.timeout, 10) || timeout) + 1000;
  const timeoutPromise = new Promise(
    (resolve => setTimeout(() => resolve({
      ok: false,
      problem: TIMEOUT_ERROR,
      originalError: 'REQUEST_TIMEOUT_ERROR',
      data: null,
      status: null,
      headers: null,
      config,
      duration
    }), duration))
  );
  return Promise.race([timeoutPromise, promise]);
};

const buildApi = (config = apiConfig) => {
  const api = create(config);
  const {
    axiosInstance: { defaults }, get, delete: del, head, post, put, patch, link, unlink
  } = api;
  api.get = (...args) => requestTimeout(get(...args), defaults, args[2]);
  api.delete = (...args) => requestTimeout(del(...args), defaults, args[2]);
  api.head = (...args) => requestTimeout(head(...args), defaults, args[2]);
  api.post = (...args) => requestTimeout(post(...args), defaults, args[2]);
  api.put = (...args) => requestTimeout(put(...args), defaults, args[2]);
  api.patch = (...args) => requestTimeout(patch(...args), defaults, args[2]);
  api.link = (...args) => requestTimeout(link(...args), defaults, args[2]);
  api.unlink = (...args) => requestTimeout(unlink(...args), defaults, args[2]);
  return api;
};

export default buildApi;

it work for me! tks a lot 💯 💯 💯

TrinhCDanh avatar Aug 04 '22 05:08 TrinhCDanh

Hello everyone, for the moment, this is my solution.

import { create, TIMEOUT_ERROR } from 'apisauce';

import apiConfig from './config';

const requestTimeout = (promise, axiosConfig, reqConfig) => {
  const { timeout } = apiConfig;
  const config = { ...axiosConfig, ...(reqConfig || {}) };
  const duration = (parseInt(config.timeout, 10) || timeout) + 1000;
  const timeoutPromise = new Promise(
    (resolve => setTimeout(() => resolve({
      ok: false,
      problem: TIMEOUT_ERROR,
      originalError: 'REQUEST_TIMEOUT_ERROR',
      data: null,
      status: null,
      headers: null,
      config,
      duration
    }), duration))
  );
  return Promise.race([timeoutPromise, promise]);
};

const buildApi = (config = apiConfig) => {
  const api = create(config);
  const {
    axiosInstance: { defaults }, get, delete: del, head, post, put, patch, link, unlink
  } = api;
  api.get = (...args) => requestTimeout(get(...args), defaults, args[2]);
  api.delete = (...args) => requestTimeout(del(...args), defaults, args[2]);
  api.head = (...args) => requestTimeout(head(...args), defaults, args[2]);
  api.post = (...args) => requestTimeout(post(...args), defaults, args[2]);
  api.put = (...args) => requestTimeout(put(...args), defaults, args[2]);
  api.patch = (...args) => requestTimeout(patch(...args), defaults, args[2]);
  api.link = (...args) => requestTimeout(link(...args), defaults, args[2]);
  api.unlink = (...args) => requestTimeout(unlink(...args), defaults, args[2]);
  return api;
};

export default buildApi;

Keep in mind that timed out response will skip api monitor if you have it set up.

Orange9000 avatar Sep 07 '22 16:09 Orange9000

do we the official resolution for this issue? cause i have used the latest version. But still got only 10 secs API timeout while im already set the timeout.

I notice that, it only happens on the released app, on development mode its working fine.

My config: image

MirolHusni avatar Feb 22 '24 08:02 MirolHusni