Home

Awesome

p-if

Conditional promise chains

Install

$ npm install p-if

Usage

import pIf from 'p-if';

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

Can also be nested:

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 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