main
Last change
on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago |
Initial commit
|
-
Property mode
set to
100644
|
File size:
1.3 KB
|
Line | |
---|
1 | import _curry2 from "./internal/_curry2.js";
|
---|
2 | import _assertPromise from "./internal/_assertPromise.js";
|
---|
3 | /**
|
---|
4 | * Returns the result of applying the onFailure function to the value inside
|
---|
5 | * a failed promise. This is useful for handling rejected promises
|
---|
6 | * inside function compositions.
|
---|
7 | *
|
---|
8 | * @func
|
---|
9 | * @memberOf R
|
---|
10 | * @since v0.26.0
|
---|
11 | * @category Function
|
---|
12 | * @sig (e -> b) -> (Promise e a) -> (Promise e b)
|
---|
13 | * @sig (e -> (Promise f b)) -> (Promise e a) -> (Promise f b)
|
---|
14 | * @param {Function} onFailure The function to apply. Can return a value or a promise of a value.
|
---|
15 | * @param {Promise} p
|
---|
16 | * @return {Promise} The result of calling `p.then(null, onFailure)`
|
---|
17 | * @see R.andThen
|
---|
18 | * @example
|
---|
19 | *
|
---|
20 | * const failedFetch = id => Promise.reject('bad ID');
|
---|
21 | * const useDefault = () => ({ firstName: 'Bob', lastName: 'Loblaw' });
|
---|
22 | *
|
---|
23 | * //recoverFromFailure :: String -> Promise ({ firstName, lastName })
|
---|
24 | * const recoverFromFailure = R.pipe(
|
---|
25 | * failedFetch,
|
---|
26 | * R.otherwise(useDefault),
|
---|
27 | * R.andThen(R.pick(['firstName', 'lastName'])),
|
---|
28 | * );
|
---|
29 | * recoverFromFailure(12345).then(console.log);
|
---|
30 | */
|
---|
31 |
|
---|
32 | var otherwise =
|
---|
33 | /*#__PURE__*/
|
---|
34 | _curry2(function otherwise(f, p) {
|
---|
35 | _assertPromise('otherwise', p);
|
---|
36 |
|
---|
37 | return p.then(null, f);
|
---|
38 | });
|
---|
39 |
|
---|
40 | export default otherwise; |
---|
Note:
See
TracBrowser
for help on using the repository browser.