我有一个用例,如果出现任何错误,我需要重试 Axios 请求 3 次,并且如果在 3 秒内没有得到任何响应,每次重试尝试应在 3 秒内超时。下面是我正在使用的代码。它会重试 3 次,但不会使每次重试尝试超时。如何让每次重试超时?任何代码片段都会有帮助。
const axiosRetry = require('axios-retry');
axiosRetry(axios, { retries: 3 });
axios.post(url,payload,{headers:header})
.then((response) =>{
console.log('Response is *****'+JSON.stringify(response));
})
.catch((err) =>{
console.log('Error occurred'+err);
});
Run Code Online (Sandbox Code Playgroud) 我有一个用例,我需要在尝试 POST 请求时 API 超时的情况下重试 Axios POST 请求。我需要在 API 超时的情况下重试 3 次,如果 POST 操作在该期间无法完成,则每个重试请求应在 4 秒内超时。我正在使用下面的代码,但重试或超时似乎都不起作用。你能告诉我有什么问题以及正确的代码片段吗?
axiosRetry(axios, { retries: 3 });
axios.post(url,payload,{headers:header},{timeout:4000})
Run Code Online (Sandbox Code Playgroud) 我正在尝试在我的节点 js 代码中使用 node-fetch 向外部 API 发布请求。
如果在执行 POST 请求时出现任何超时或网络故障,我想重试该请求 3 次。
你能告诉我如何使用 node-fetch 重试请求吗?我看到有一个 npm 模块https://www.npmjs.com/package/node-fetch-retry但它似乎没有按预期工作,而且它也不接受重试之间的重试间隔。任何代码片段都会非常有帮助。
编辑:
谢谢我尝试使用 promise-fn-retry 但它似乎没有做任何重试。下面是我通过关闭 WIFI 然后执行 FETCH 调用以查看它是否重试 3 次而尝试的代码片段。但它只执行一次获取并返回错误。
const promiseFn = () => fetch(url,{method:"POST",headers:header,body:JSON.stringify(payload)});
const options = {
times: 3,
initialDelay: 100,
onRetry: (error) => {
console.log(error);
}
};
console.log('PromiseFn result ****'+retry(promiseFn, options).then(res => res.json()).then((res)=>{console.log('Promise Fn result is'+JSON.stringify(res))}).catch((err)=>{console.log('Error in Promise Fn'+err)}));
Run Code Online (Sandbox Code Playgroud)