1.0.0 • Published 5 years ago

async-iterator-retry v1.0.0

Weekly downloads
2
License
Unlicense
Repository
github
Last release
5 years ago

async-iterator-retry npm version

Retry async iteraotrs.

Overview

Like promise-retry but for async iterators, also based on retry.

Usage

const asyncIteratorRetry = require('async-iterator-retry')

const options = {
  retries: 10, // The maximum amount of times to retry the operation. Default is 10.
  factor: 2, // The exponential factor to use. Default is 2.
  minTimeout: 1000, // The number of milliseconds before starting the first retry. Default is 1000.
  maxTimeout: Infinity, // The maximum number of milliseconds between two retries. Default is Infinity.
  randomize: false // Randomizes the timeouts by multiplying with a factor between 1 to 2. Default is false.
}

const iteratorWithRetry = asyncIteratorRetry(async function * (retry, attempt) {
  if (attempt > 1) {
    console.log(`Retrying, attempt ${attempt}...`)
  }

  const iterator = getSomeIterator()

  try {
    yield * iterator
  } catch (err) {
    if (err.code === 'ETIMEDOUT') {
      yield * retry(err)
    }

    throw err
  }

}, options)

for await const (item of myIteratorWithRetry) {
  console.log(item)
}