1.1.1 • Published 3 years ago

like-retry v1.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

like-retry

Retry and backoff using generators.

npm.io npm.io npm.io npm.io

npm i like-retry

Usage

const retry = require('like-retry')

for await (const backoff of retry({ max: 3, delay: 3000 })) {
  try {
    const response = await axios.get(...)
    console.log(response.data)
  } catch (error) {
    console.log(backoff.left) // 3, 2, 1, 0
    await backoff(error) // 3s, 3s, 3s and finally throws
  }
}

Jitter

for await (const backoff of retry({ max: 5, delay: 1000, jitter: 500 })) {
  await backoff(new Error()) // 1489ms, 1142ms, 1276ms, 1088ms, 1337ms and finally throws
}

Linear

for await (const backoff of retry({ max: 5, delay: 3000, strategy: 'linear' })) {
  await backoff(new Error()) // 3s, 6s, 9s, 12s, 15s and finally throws
}

Exponential

for await (const backoff of retry({ max: 5, delay: 20, strategy: 'exponential' })) {
  await backoff(new Error()) // 0.4s, 1.6s, 3.6s, 6.4s, 10s and finally throws
}

Array

for await (const backoff of retry({ max: 5, strategy: [1000, 5000, 15000] })) {
  await backoff(new Error()) // 1s, 5s, 15s, 15s, 15s and finally throws
}

Custom

const strategy = ({ delay, count, jitter }) => delay ** count

for await (const backoff of retry({ max: 5, delay: 2, strategy })) {
  await backoff(new Error()) // 4ms, 16ms, 64ms, 256ms, 1024ms and finally throws
}

None

It's the same as no setting any strategy.

for await (const backoff of retry({ max: 3, delay: 3000, strategy: 'none' })) {
  await backoff(new Error()) // 3s, 3s, 3s and finally throws
}

Default

Without options there will be no retries, delay or anything.

for await (const backoff of retry()) {
  await backoff(new Error()) // will just throw
}

License

MIT

1.1.1

3 years ago

1.1.0

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago