-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.d.ts
32 lines (27 loc) · 1.13 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
Conditional promise chains. It's just a passthrough if `condition` is `false` and `doElse` is not provided.
@param condition - Decides whether `doIf` or `doElse` is executed.
@param doIf - Executed if `condition` is `true`. Expected to return a `Promise` or value.
@param doElse - Executed if `condition` is `false`. Expected to return a `Promise` or value.
@returns A [thunk](https://en.wikipedia.org/wiki/Thunk) that returns a `Promise`.
@example
```
import pIf from 'p-if';
getData()
.then(pIf(process.env.NODE_ENV !== 'production', addDebugInfo))
.then(data => {
console.log(data);
});
// Can also be nested:
getList()
.then(pIf(shouldSort, pIf(sortDirection === 'ascending', sort.asc, sort.desc)))
.then(list => {
console.log(list);
});
```
*/
export default function pIf<ValueType, DoIfReturnType, DoElseReturnType = ValueType>(
condition: boolean | ((value: ValueType) => boolean | PromiseLike<boolean>),
doIf: (value: ValueType) => DoIfReturnType | PromiseLike<DoIfReturnType>,
doElse?: (value: ValueType) => DoElseReturnType | PromiseLike<DoElseReturnType>
): (value: ValueType) => Promise<DoIfReturnType | DoElseReturnType>;