# p-catch-if

> Conditional promise catch handler

Latest version **3.0.0** (published 2021-04-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install p-catch-if
pnpm add p-catch-if
yarn add p-catch-if
bun add p-catch-if
```

## Health

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

Positive: has types package; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.0 |
| Published | 2021-04-09 |
| First published | 2016-10-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/p-catch-if) |
| Module format | ESM |
| Node | >=12 |
| Dependencies | 1 |
| Unpacked size | 6.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 40 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | promise, catch, if, conditional, cond, iff, chain, throw, error, reject, rejected, handler, filter, filtered, predicate, exception, thunk, function, async, await, promises, combinator, bluebird |

## Links

- npm: https://www.npmjs.com/package/p-catch-if
- Repository: https://github.com/sindresorhus/p-catch-if
- Homepage: https://github.com/sindresorhus/p-catch-if#readme
- Issues: https://github.com/sindresorhus/p-catch-if/issues
- Funding: https://github.com/sponsors/sindresorhus
- npm.io page: https://npm.io/package/p-catch-if

## Dependencies (1)

- [is-error-constructor](https://npm.io/package/is-error-constructor.md) ^3.0.0

## Alternatives

- [@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
- [fa](https://npm.io/package/fa.md) — 185 weekly downloads

## Recent versions

- 3.0.0 (latest) — 2021-04-09
- 2.1.0 — 2019-04-04
- 2.0.0 — 2019-03-12
- 1.0.2 — 2017-04-17
- 1.0.1 — 2016-10-22
- 1.0.0 — 2016-10-22

## README

# p-catch-if

> Conditional promise catch handler

Useful for handling only some types of errors and let the rest pass through.

## Install

```
$ npm install p-catch-if
```

## Usage

```js
import pCatchIf from 'p-catch-if';

// Error constructor
getData().catch(pCatchIf(TimeoutError, () => retry(getData)));

// Multiple error constructors
getData().catch(pCatchIf([NetworkError, TimeoutError], () => retry(getData)));

// Boolean
getData().catch(pCatchIf(isProduction, error => recordError(error)));

// Function
const hasUnicornMessage = error => error.message.includes('unicorn');
getData().catch(pCatchIf(hasUnicornMessage, console.error));

// Promise-returning function
const handler = error => sendError(error).then(checkResults);
getData().catch(pCatchIf(handler, console.error));

// Can also be nested
const validateMessage = error => error.message === 'Too many rainbows';
getData().catch(pCatchIf(UnicornError, pCatchIf(validateMessage, console.error)));
```

## API

### pCatchIf(predicate, catchHandler)

Returns a [thunk](https://en.wikipedia.org/wiki/Thunk) that returns a `Promise`.

#### predicate

Type: `Error.constructor` `Error.constructor[]` `boolean` `Function -> Promise<boolean>|boolean`

Specify either an error constructor, array of error constructors, boolean, or function that returns a promise for a boolean or a boolean.

If the function returns a promise, it's awaited.

#### catchHandler

Type: `Function`

Called if `predicate` passes.

This is what you would normally pass to `.catch()`.

## Related

- [p-if](https://github.com/sindresorhus/p-if) - Conditional promise chains
- [p-tap](https://github.com/sindresorhus/p-tap) - Tap into a promise chain without affecting its value or state
- [p-log](https://github.com/sindresorhus/p-log) - Log the value/error of a promise
- [More…](https://github.com/sindresorhus/promise-fun)

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