# promise-mate

> Delegate async responses

Latest version **1.1.0** (published 2019-02-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install promise-mate
pnpm add promise-mate
yarn add promise-mate
bun add promise-mate
```

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2019-02-12 |
| First published | 2018-11-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 20.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | czycha |
| Maintainers | czycha |
| Keywords | promise, delegation |

## Links

- npm: https://www.npmjs.com/package/promise-mate
- Repository: https://github.com/czycha/promise-mate
- npm.io page: https://npm.io/package/promise-mate

## Dependencies (2)

- [fast-deep-equal](https://npm.io/package/fast-deep-equal.md) ^2.0.1
- [just-is-primitive](https://npm.io/package/just-is-primitive.md) ^1.1.0

## 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.1.0 (latest) — 2019-02-12
- 1.0.0 — 2018-11-27

## README

# promise-mate

`Promise.all` fulfillment for many `Promises` to many callbacks to prevent duplicate calls when there are shared requirements.

## Use cases

Use promise-mate if you have multiple `Promises` to run synchronously that are required by multiple callbacks, some of which have overlapping requirements. This is particularly useful for modular callbacks that may not always be called as well as for resource fetching.

## Installation

```bash
npm i promise-mate
```

Or,

```bash
yarn add promise-mate
```

## Example

The below example fetches data to update the webpage.

```js
import Mate from 'promise-mate'

const runner = new Mate({
  users() {
    return fetch('/api/users').then(res => res.json()).then(json => json.users)
  },
  purchases() {
    return fetch('/api/purchases').then(res => res.json()).then(json => json.purchases)
  },
  products() {
    return fetch('/api/products').then(res => res.json()).then(json => json.products)
  },
  calendar() {  // Note: this Promise definition is not requested below so will not be run
    return fetch('/api/calendar').then(res => res.json()).then(json => json.calendar)
  }
});

runner
  .all([
    {
      requires: 'users',
      then(users) {
        // Update the user list!
      }
    },
    {
      requires: ['products', 'purchases'],
      then(products, purchases) {
        // Update product list to show if something's been bought
      }
    },
    {
      requires: [{ uuid: 1265723 }, 'users', 'products', 'purchases'],
      then(userData, users, products, purchases) {
        // Show user's latest purchases
      }
    }
  ])
  .then(() => { console.log('Page updated!') })
  .catch(() => { console.error('Error updating page') })
```

## API

### `Mate.all`

Run all actions.

```js
Mate.all(definitions, actions)
```

#### Arguments

- **`definitions`** - Key-indexed object with values that are functions that return Promises (or something else).
- **`actions`** - Array of objects defining requirements and callbacks.
  - **`actions[i].requires`** - Requirements to be passed as arguments to the callback. Can be a singular requirement or an array of requirements. Singular requirements cannot be an array. If a requirement is not a key in `definitions`, will resolve the requirement.
  - **`actions[i].then`** - Function to call when requirements resolve. Passes results as arguments in order of `actions[i].requires`.

#### Returns

- **`Promise<any[]>`** - An array of what each action's callback returns.

### `new Mate`

Create a `Mate` with stored definitions.

```js
var runner = new Mate(definitions)
```

#### Arguments

- **`definitions`** (optional) - Key-indexed object with values that are functions that return Promises (or something else).

### `Mate.prototype.define`

Store a Promise generating function.

```js
runner.define(key, definition)
```

#### Arguments

- **`key`** - Key for definition.
- **`definition`** - Function that returns a `Promise` (or something else since `Promise.all` will just resolve non-promises).

#### Returns

- The `Mate` instance.

### `Mate.prototype.undefine`

Delete a definition

```js
runner.undefine(key)
```

### Arguments

- **`key`** - Key for deletion.

### Returns

- The `Mate` instance.

### `Mate.prototype.all`

Run all actions.

```js
runner.all(actions)
```

#### Arguments

- **`actions`** - Array of objects defining requirements and callbacks.
  - **`actions[i].requires`** - Requirements to be passed as arguments to the callback. Can be a singular requirement or an array of requirements. Singular requirements cannot be an array. If a requirement is not a key in `definitions`, will resolve the requirement.
  - **`actions[i].then`** - Function to call when requirements resolve. Passes results as arguments in order of `actions[i].requires`.

#### Returns

- **`Promise<any[]>`** - An array of what each action's callback returns.

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