Retry
失败自动重试,可用于 http 请求重试,或其他需要重试的逻辑
Install
npm i @4a/retry
Usage
import { Retry } from '@4a/retry'
const { Retry } = require('@4a/retry')
Example
let i = 0
Retry.delay(300)
.max(10)
.when((result) => result !== 10)
.repeat(() => {
i++
return i
})
.then((result) => {
console.log('result:', result)
})
.catch((err) => {
console.error('error:', err)
})
Simple
let i = 0
Retry.when((result) => result !== 10)
.repeat(() => {
i++
return i
})
.then((result) => {
console.log('result:', result)
})
.catch((err) => {
console.error('error:', err)
})
Axios Request
Retry.when((result) => result.data.code !== 0)
.repeat(() => axios(options))
.then((result) => {
console.log('result:', result)
})
.catch((err) => {
console.error('error:', err)
})
npm test