有没有办法https.get
使用 async-retry 为 node.js 中的方法实现重试。
问问题
1389 次
1 回答
0
如果您使用此模块https://github.com/zeit/async-retry 您的答案在 README.md 文件中
// Packages
const retry = require('async-retry')
const fetch = require('node-fetch')
await retry(async bail => {
// if anything throws, we retry
const res = await fetch('https://google.com')
if (403 === res.status) {
// don't retry upon 403
bail(new Error('Unauthorized'))
return
}
const data = await res.text()
return data.substr(0, 500)
}, {
retries: 5
})
如果你想要更流行的解决方案/npm 模块,你可以在这里找到它https://www.npmjs.com/package/requestretry
const request = require('requestretry');
...
// use await inside async function
const response = await request.get({
url: 'https://api.domain.com/v1/a/b',
json: true,
fullResponse: true, // (default) To resolve the promise with the full response or just the body
// The below parameters are specific to request-retry
maxAttempts: 5, // (default) try 5 times
retryDelay: 5000, // (default) wait for 5s before trying again
retryStrategy: request.RetryStrategies.HTTPOrNetworkError // (default) retry on 5xx or network errors
})
于 2019-04-05T12:29:55.593 回答