p-if

Conditional promise chains

Usage no npm install needed!

<script type="module">
  import pIf from 'https://cdn.skypack.dev/p-if';
</script>

README

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ā€¦