Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何使用节点获取重试api调用_Node.js_Node Fetch - Fatal编程技术网

Node.js 如何使用节点获取重试api调用

Node.js 如何使用节点获取重试api调用,node.js,node-fetch,Node.js,Node Fetch,我试图在我的NodeJS代码中使用node fetch将请求发布到外部API。 如果在执行POST请求时出现任何超时或网络故障,我想重试请求3次。 您能告诉我如何使用节点获取重试请求吗?我看到有一个npm模块,但它似乎没有按预期工作,而且它也不接受重试之间的重试间隔。任何代码片段都将非常有用 const promiseFn = () => fetch(url,{method:"POST",headers:header,body:JSON.stringify(payload)}); cons

我试图在我的NodeJS代码中使用node fetch将请求发布到外部API。 如果在执行POST请求时出现任何超时或网络故障,我想重试请求3次。 您能告诉我如何使用节点获取重试请求吗?我看到有一个npm模块,但它似乎没有按预期工作,而且它也不接受重试之间的重试间隔。任何代码片段都将非常有用

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)}));
编辑:

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)}));

谢谢,我尝试过使用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)}));

这段话曾经帮助过我!希望这就是你要找的

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)}));
const fetch = require('node-fetch')

const delay = ms => {
    return new Promise(resolve => {
        setTimeout(() => {
            resolve()
        }, ms)
    })
}
const retryFetch = (url, fetchOptions = {}, retries = 3, retryDelay = 1000, timeout) => {
    return new Promise((resolve, reject) => {

        // check for timeout
        if(timeout) {
          setTimeout(() => {
            reject('error: timeout') // reject if over time
          }, timeout)
        }

        const wrapper = n => {
            fetch(url, fetchOptions)
                .then(res => { resolve(res) })
                .catch(async err => {
                    if(n > 0) {
                        // console.log(`retrying ${n}`)
                        await delay(retryDelay)
                        wrapper(--n)
                    } else {
                        reject(err)
                    }
                })
        }

        wrapper(retries)
    })
}

retryFetch('http://localhost:8080/test', {}, 20)
    .then(res => res.text())
    .then(console.log)
    .catch(console.error)

这段话曾经帮助过我!希望这就是你要找的

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)}));
const fetch = require('node-fetch')

const delay = ms => {
    return new Promise(resolve => {
        setTimeout(() => {
            resolve()
        }, ms)
    })
}
const retryFetch = (url, fetchOptions = {}, retries = 3, retryDelay = 1000, timeout) => {
    return new Promise((resolve, reject) => {

        // check for timeout
        if(timeout) {
          setTimeout(() => {
            reject('error: timeout') // reject if over time
          }, timeout)
        }

        const wrapper = n => {
            fetch(url, fetchOptions)
                .then(res => { resolve(res) })
                .catch(async err => {
                    if(n > 0) {
                        // console.log(`retrying ${n}`)
                        await delay(retryDelay)
                        wrapper(--n)
                    } else {
                        reject(err)
                    }
                })
        }

        wrapper(retries)
    })
}

retryFetch('http://localhost:8080/test', {}, 20)
    .then(res => res.text())
    .then(console.log)
    .catch(console.error)
尝试该包,它具有指定重试时间、初始延迟时间和回调方法的属性,以处理失败的获取/承诺

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)}));
希望这有帮助

尝试该包,它具有指定重试时间、初始延迟时间和回调方法的属性,以处理失败的获取/承诺

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)}));

希望这有帮助

谢谢,我尝试过使用promise-fn-retry,但它似乎没有进行任何重试。我编辑了问题以添加我用于promise-fn-retry的代码段。谢谢,我尝试过使用promise-fn-retry,但它似乎没有进行任何重试。我编辑了问题以添加用于promise-fn-retry的代码段。这很好,但有什么方法可以做到吗指定超时?您好,这是一个“快速且不干净”的超时解决方案。如果答案适合您,请将其标记为已接受。希望对您有所帮助:这很好,但是我们有没有办法指定一个超时?您好,这是一个“快速而肮脏”的超时解决方案。如果答案适合您,请将其标记为已接受。希望对您有所帮助:
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)}));