p-if
TypeScript icon, indicating that this package has built-in type declarations

3.0.0 • Public • Published

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

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

Dependencies (0)

    Dev Dependencies (3)

    Package Sidebar

    Install

    npm i p-if

    Weekly Downloads

    522

    Version

    3.0.0

    License

    MIT

    Unpacked Size

    4.8 kB

    Total Files

    5

    Last publish

    Collaborators

    • sindresorhus