# batch-promises-with-delay

> Promise batching for unreliable api calls that have rate limits

Latest version **1.0.6** (published 2020-07-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install batch-promises-with-delay
pnpm add batch-promises-with-delay
yarn add batch-promises-with-delay
bun add batch-promises-with-delay
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.6 |
| Published | 2020-07-06 |
| First published | 2020-01-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 14.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | joeltok |
| Maintainers | joeltok |
| Keywords | promise, batch, delay, retry, retries |

## Links

- npm: https://www.npmjs.com/package/batch-promises-with-delay
- Repository: https://github.com/joeltok/batch-promises-with-delay
- Homepage: https://github.com/joeltok/batch-promises-with-delay#readme
- Issues: https://github.com/joeltok/batch-promises-with-delay/issues
- npm.io page: https://npm.io/package/batch-promises-with-delay

## Alternatives

- [promise-throttle](https://npm.io/package/promise-throttle.md) — 44.2K weekly downloads
- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads

## Recent versions

- 1.0.6 (latest) — 2020-07-06
- 1.0.5 — 2020-04-05
- 1.0.4 — 2020-04-05
- 1.0.3 — 2020-01-02
- 1.0.2 — 2020-01-01
- 1.0.1 — 2020-01-01
- 1.0.0 — 2020-01-01

## README

# Batch Promises with Delay

This module is for batching promises that need delays between batches. It also allows users to retry promises that have failed. One situation this can be useful is when dealing with unreliable api calls that have rate limits.

## Installation

```sh
npm install --save batch-promises-with-delay
```

or

```sh
yarn add batch-promises-with-delay
```

## Simple Usage

```js
// simple-example.js

const batchPromisesWithDelay = require('batch-promises-with-delay')

const promises = [
  {
    func: async (a, b) => {
      /* some api call */
      /* some database save call */
    },
    args: ['A', 'B'],
  },
  {
    func: async (c, d) => {
      /* some api call */
      /* some database save call */
    },
    args: ['C', 'D'],
  },
]

const options = {
  batchSize: 3,
  delayBetweenBatches: 1000, // in ms
}

batchPromisesWithDelay(promises, options)
```


## Options

| Property            | Description                           | Type               | Default Value |
|---------------------|---------------------------------------|--------------------|---------------|
| batchSize           | Size of the batch                     | number             | `1`           |
| delayBetweenBatches | Time delay between batches in ms      | number             | `1`           |
| retryFilters        | Filters to enable retry (see below)   | array of functions | `[]`          |
| greedyRetry         | Retry all promises, even if they fail | boolean            | `false`       |
| ignoreFailures      | Quietly ignore all promise failures   | boolean            | `false`       |
| onSuccess           | Lifecycle hook (see below)            | f(args, result)    | `() => {}`    |
| onFailure           | Lifecycle hook (see below)            | f(args, err)       | `null`        |
| onRetry             | Lifecycle hook (see below)            | f(args)            | `() => {}`    |
| onBatchStart        | Lifecycle hook (see below)            | f()                | `() => {}`    |
| onBatchEnd          | Lifecycle hook (see below)            | f()                | `() => {}`    |

## Retrying Promises

In certain circumstances, there will be a need to retry promises when they throw a fail condition. Such promises are thrown to the back of the queue to be retried.

The way we decide if a fail condition should be retried is by using an array of filters. As long as one filter in an array passes, the promise is retried.

```js
const retryFilters = [
  (err) => {
    err.message == 'retry me'
  },
  (err) => {
    err.statusCode == 418
  },
]
```

## Lifecycle hooks

These are functions that are called at certain points in the lifecycle of each batch or promise.

The following arguments are worth mention:
- `args` refers to the arguments that are passed as part of the "promises". The reason this is here is partly to give context to the result, and partly to identify which promises are failing by providing some data for logging.
- `result` is the result that is gotten from the promise that has been executed.
- `err` is the error object.

---
_Source: https://npm.io/package/batch-promises-with-delay · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
