Skip to content
Conditional promise chains
TypeScript JavaScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.editorconfig Require Node.js 8, add TypeScript definition (#4) Mar 12, 2019
.gitattributes
.gitignore
.npmrc
.travis.yml Require Node.js 8, add TypeScript definition (#4) Mar 12, 2019
index.d.ts
index.js
index.test-d.ts
license
package.json
readme.md Meta tweaks Mar 12, 2019
test.js

readme.md

p-if Build Status

Conditional promise chains

Install

$ npm install p-if

Usage

const pIf = require('p-if');

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

Can also be nested:

const pIf = require('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 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 - Conditional promise catch handler
  • p-log - Log the value/error of a promise
  • p-tap - Tap into a promise chain without affecting its value or state
  • More…

License

MIT © Sindre Sorhus

You can’t perform that action at this time.