0

我遇到了在有限网络期间rnfb挂起并且不会始终产生超时的问题。这是一种不好的处理方式吗?

return new Promise((res, err) => {
    let time;
    const task = RNFetchBlob.fetch(
      'PUT',
      `${API_HOST}${path}`,
      {
        'Content-Type': `image/${fileType}`,
        Authorization: `Bearer ${token}`
      },
      RNFetchBlob.wrap(file)
    );

    task
      .then((result) => {
        clearTimeout(time);
        const resCode = result.info()?.status;
        if (resCode !== 200) {
          err(new Error(`Upload delivery proof failed with status code ${resCode}`));
        }
        res(result);
      })
      .catch((e) => {
        clearTimeout(time);
        err(e);
      });

    time = setTimeout(() => {
      task.cancel();
    }, 10000);
  });
4

0 回答 0