npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

batch-retry

v0.1.1

Published

[![Build Status](https://travis-ci.org/chux0519/batch-retry.svg?branch=master)](https://travis-ci.org/chux0519/batch-retry) [![codecov](https://codecov.io/gh/chux0519/batch-retry/branch/master/graph/badge.svg)](https://codecov.io/gh/chux0519/batch-retry)

Downloads

9

Readme

batch-retry

Build Status codecov

Helps you to retry a batch of tasks.

Feature

  • Only retry failed tasks.
  • Retry results can be cached and eventually all returned if you want.
  • You can customize the maximum number of retries.
  • You can customize whether the result should be retried.

Install

npm install --save batch-retry

yarn add batch-retry

Usage

Using BatchRetry class

It provide a class called BatchRetry which should be contructed with a executor.

The executor is a function that takes an array of size n as input, and returns another array of size n as output.

A BatchRetry instance has a run method, which will pass param to executor and manage the retry process.

When the executor itself throws, the run method will throws too, so executor must catch all tasks' error and return something for checking retry. You can simplily return the error, BatchRetry will check the result, if it is an error(by default, I will discuss this later), it would be recorded and wait for batch retry.

const {BatchRetry} = require('batch-retry')

const evenError = new Error('Even')
const simpleExecutor = numbers => Promise.all(numbers
  .map(each =>
    (each % 2 === 0)
      ? Promise.reject(evenError).catch(e => e) // have to catch by hand or it will throw
      : Promise.resolve(each)
  )
)

// Initialize a instance.
const batchRetry = new BatchRetry({
      maxRetries: 5,
      onlyFinalResult: false, // return all the result
      executor: simpleExecutor
    })
const input = [1, 2, 3]

// Run the process
batchRetry.run(input).then(console.log)
// will output all the result [[1],[evenError, evenError, evenError, evenError, evenError],[3]]
// if onlyFinalResult set to true, it will only output [1, evenError, 3]

Using buildWithBatchRetry function

It provide a helper function buildWithBatchRetry, to compose high order functions.

const {buildWithBatchRetry} = require('batch-retry')

const evenError = new Error('Even')
const simpleExecutor = numbers => Promise.all(numbers
  .map(each =>
    (each % 2 === 0)
      ? Promise.reject(evenError).catch(e => e) // have to catch by hand or it will throw
      : Promise.resolve(each)
  )
)

// case 1: use it as a wrapper function
{
  const withBatchRerty = buildWithBatchRetry({
        maxRetries: 5,
        onlyFinalResult: false // return all the result
      })
  // wrap the executor
  const simpleExecutorWithBatchRetry = withBatchRerty(simpleExecutor)
  // Run the process
  simpleExecutorWithBatchRetry([1, 2, 3]).then(console.log)
  // will output all the result [[1],[evenError, evenError, evenError, evenError, evenError],[3]]
  // if onlyFinalResult set to true, it will only output [1, evenError, 3]
}

// case 2: use it directly
{
  const simpleExecutorWithBatchRetry = buildWithBatchRetry({
        maxRetries: 5,
        onlyFinalResult: false, // return all the result
        executor: simpleExecutor
      })
  // Run the process
  simpleExecutorWithBatchRetry([1, 2, 3]).then(console.log)
}

API

BatchRetry

constructor(options) => batchRetry: BatchRetry

  • options.shouldRetry: Function for checking retry. Default: Check if the value is of type error or error-like(has properties stack and message) or executed times is greater than or euqal to maxRetries.
    • Function: (task, result, executedTimes) = > Boolean
  • options.maxRetries: Max retry times. Default: 3
    • Number
  • options.onlyFinalResult: Representing if it should only returns the final result. If setting to false, it will returns all results instead. For example, having final result: [1, error, 3] and all results: [[1], [error, error, error, error, error], [3]], it means only the second task failed, and after five retries, it still failed. Default: true
    • Boolean
  • options.executor: Executor function. It takes an array of size n as input, and returns another array of the same size as output. Required
    • Function: tasks: Array => result: Array