Socket
Socket
Sign inDemoInstall

@4a/retry

Package Overview
Dependencies
9
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @4a/retry

失败自动重试,可用于 http 请求重试,或其他需要重试的逻辑


Version published
Weekly downloads
5
increased by400%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

Retry

失败自动重试,可用于 http 请求重试,或其他需要重试的逻辑

Install

npm i @4a/retry

Usage

// ES6
import { Retry } from '@4a/retry'

// Nodejs
const { Retry } = require('@4a/retry')

Example

let i = 0

Retry.delay(300) // 可选调用 重试间隔300ms,默认300ms,单位ms
  .max(10) // 可选调用 重试次数10,默认3次防止死循环
  .when((result) => result !== 10) // 必须调用 重试条件,符合条件则重试,否则视为成功正常返回
  .repeat(() => {
    // 支持async函数,支持promise,返回Promise
    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

Keywords

FAQs

Last updated on 20 Jul 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc