# p-if

> Conditional promise chains

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

## Install

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

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.0 |
| Published | 2021-04-09 |
| First published | 2016-10-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM |
| Node | >=12 |
| Dependencies | 0 |
| Unpacked size | 4.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 64 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | promise, if, else, conditional, cond, iff, chain, thunk, function, async, await, promises, combinator, bluebird |

## Links

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

## 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-05
- 2.0.0 — 2019-03-12
- 1.1.0 — 2016-11-03
- 1.0.0 — 2016-10-21

## README

# p-if

> Conditional promise chains

## Install

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

## Usage

```js
import pIf from 'p-if';

getData()
	.then(pIf(process.env.NODE_ENV !== 'production', addDebugInfo))
	.then(data => {
		console.log(data);
	});
```

Can also be nested:

```js
import pIf from 'p-if';

getList()
	.then(pIf(shouldSort, pIf(sortDirection === 'ascending', sort.asc, sort.desc)))
	.then(list => {
		console.log(list);
	});
```


## API

### pIf(condition, doIf, doElse?)

It's just a passthrough if `condition` is `false` and `doElse` is not provided.

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

#### condition

Type: `boolean | Function`

Decides whether `doIf` or `doElse` is executed.

Can be a `boolean`, or a `Function` returning a `boolean` or a `Promise` for a `boolean`.

#### doIf

Type: `Function`

Executed if `condition` is `true`.

Expected to return a `Promise` or value.

#### doElse

Type: `Function`

Executed if `condition` is `false`.

Expected to return a `Promise` or value.

## Related

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

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