reactjs - React 如何使用 setTimeout 处理递归 API 调用
问题描述
我正在进行 API 调用,如果失败,我需要在设定的时间后再次调用 API,但是,由于超时范围,我无法清除超时。
这是有效的,但可以清除超时:
React.useEffect(() => {
if (progress === 45 && !apiCallFailedRef.current) {
callApi();
}
function callApi(){
let requestData = {
method: 'GET',
path,
};
$.ajax(testPath, {
method: 'GET',
data: { data: JSON.stringify(requestData) },
})
.done(response => {
// setLoading(false);
})
.fail(response => {
console.log('failed')
apiCallFailedRef.current = true;
setTimeout(() => callApi(), 28000);
});
}
};
// How would I clear the timeout correctly?
// return () => {
// clearTimeout();
// };
}, [progress])
解决方案
我建议不要永远重试,您可以使用特定的重试次数,并且每次重试也可以等待更长的时间再重试。
这将进行 api 调用,最多重试 5 次
function callApiWithRetry(url, options = {}, numRetries = 5, backoff = 250) {
return fetch(url, options)
.then(res => {
// if successful, return JSON response
if (res.ok) {
return res.json();
}
// not successful, retry until numRetry is 0
if (numRetries > 0) {
// make the api call again, but reduce the retry count, and double the backoff time
setTimeout(() => {
return callApiWithRetry(url, options, numRetries - 1, backoff * 2)
}, backoff);
} else {
// out of retries, something is wrong
throw new Error(res);
}
})
.catch(console.error);
}
推荐阅读
- php - 无法运行作曲家自我更新:“phar 签名与您下载的文件不匹配”
- python - BeautifulSoup 从输出中选择一行
- php - 在获取给定日期的下个月时使用修改的正确方法
- hive - 引起:java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkArgument(ZLjava/lang/String;Ljava/lang/Object;)V
- sql - 在表中查找具有 2 个特定尺寸的项目
- android - 如何将我的 Android 应用程序连接到后端?
- perl - sed 未终止地址正则表达式
- javascript - 如何在调用promise的for循环外访问Map列表
- database - 如何在数据库中设置不同的消息帖子类型
- c# - 从实例化对象访问其他脚本方法