# promise-composer

> A tool allowing simple conditional composition with promises in JS

Latest version **0.3.11** (published 2018-10-03) · ISC license · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.11 |
| Published | 2018-10-03 |
| First published | 2018-09-04 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 10.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | FalconPilot |
| Maintainers | falconpilot |
| Keywords | promise, js, javascript |

## Links

- npm: https://www.npmjs.com/package/promise-composer
- Repository: https://github.com/FalconPilot/promise-composer
- Homepage: https://github.com/FalconPilot/promise-composer#readme
- Issues: https://github.com/FalconPilot/promise-composer/issues
- npm.io page: https://npm.io/package/promise-composer

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 0.3.11 (latest) — 2018-10-03
- 0.3.10 — 2018-10-03
- 0.3.9 — 2018-10-03
- 0.3.8 — 2018-10-03
- 0.3.7 — 2018-09-21
- 0.3.6 — 2018-09-20
- 0.3.5 — 2018-09-20
- 0.3.3 — 2018-09-19
- 0.3.1 — 2018-09-19
- 0.3.0 — 2018-09-19
- 0.2.2 — 2018-09-19
- 0.2.1 — 2018-09-18
- 0.2.0 — 2018-09-18
- 0.1.2 — 2018-09-12
- 0.1.1 — 2018-09-04
- … 1 more at https://npm.io/package/promise-composer/versions

## README

![Tests coverage](https://raw.githubusercontent.com/FalconPilot/promise-composer/master/badges/tests.png)

# How to install ?

1. Run `npm install promise-composer`
2. Include the Promise Composer Object (PCO) with something like :

```javascript
var PCO = require('promise-composer');
```

# What does it do ?

Promise Composer is a small JS library that gives some utility tools and
functions to help asynchronous processes to dynamically check/validate data.

I wrote it since I was tired of checking type errors manually using the
same old `if/else` or `switch/case` methods, so instead of writing :

```javascript
if (myCheckingFunc(value) && myOtherFunc(value)) {
  doStuff()
} else {
  doOtherStuff()
}
```

You could just write :

```javascript
Promise.resolve(value)
  .then(PCO.myCheckingFunc)
  .then(PCO.myOtherFunc)
  .then(doStuff)
  .catch(doOtherStuff)
```

It still is pretty minimal, but I do think this can be of help to ensure a
layer of dynamical verification without having to cope with `if/else` forests
or huge monolithic `switch/case` blocks.

# Create an assertion

To create a custom assertion function, you only have to use the `PCO.assert`
function.

```javascript
function validateObject(x) {
  return Array.isArray(x)
    && x.length > 3
    && x.length < 9
}

Promise.resolve(15)
  .then(x => PCO.assert(x, validateObject))
  .then(doOtherStuff)
  .catch(computeError)
```

# Common assertion functions

• `PCO.isset`
Check if an element isn't undefined

• `PCO.exists`
Check if an element isn't undefined and is non-null

• `PCO.fullString`
Check if an element is a non-empty Sstring (trims whitespaces)

• `PCO.isObject`
Check if an element is a non-array Object

• `PCO.isArray`
Check if an element is a valid Array

• `PCO.isNumber`
Check if an element is a valid Number

• `PCO.isInteger`
Check if an element is a valid Integer

# Multiple assertions

If you need multiple validations at once, you can use two important functions :

• `PCO.any`
This functions acts as an "or" operator. It will proceed if any of the
given assertions is true.

*Example*
```javascript
Promise.resolve("Coucou")
  .then(x => PCO.any(x, [PCO.fullString, PCO.isset]))
  .then(doStuff)
  .catch(computeError)

// This example should not trigger the .catch() block
```

• `PCO.all`
This function acts as an "and" operator. It will proceed if all of the
given assertions are true.

*Example*
```javascript
Promise.resolve(15)
  .then(x => PCO.all(x, [PCO.fullString, PCO.isset]))
  .then(doStuff)
  .catch(computeError)

// This example should trigger the .catch() block !
```

*Important !*

`PCO.any` and `PCO.all` both require as a second argument an array of
assertions. You can use standard PCO assertion functions as well as
custom assertion functions (this might be useful for dataset validation
for instance).

# Verify assertion on multiple values

You can use `PCO.traverse` using an array of values instead of a single value
to check every value inside the array with your assertion.

*Example*
```javascript
Promise.resolve(["foo", "bar", null])
  .then(x => PCO.traverse(x, PCO.exists))
  .then(doStuff)
  .catch(computeError)

// This exemple should trigger the .catch() block !
```

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