mirror of
https://github.com/swc-project/swc.git
synced 2024-12-21 04:32:01 +03:00
42 lines
1.3 KiB
TypeScript
42 lines
1.3 KiB
TypeScript
// Loaded from https://deno.land/x/ramda@v0.27.2/source/otherwise.js
|
|
|
|
|
|
import _curry2 from './internal/_curry2.js';
|
|
import _assertPromise from './internal/_assertPromise.js';
|
|
|
|
|
|
/**
|
|
* Returns the result of applying the onFailure function to the value inside
|
|
* a failed promise. This is useful for handling rejected promises
|
|
* inside function compositions.
|
|
*
|
|
* @func
|
|
* @memberOf R
|
|
* @since v0.26.0
|
|
* @category Function
|
|
* @sig (e -> b) -> (Promise e a) -> (Promise e b)
|
|
* @sig (e -> (Promise f b)) -> (Promise e a) -> (Promise f b)
|
|
* @param {Function} onFailure The function to apply. Can return a value or a promise of a value.
|
|
* @param {Promise} p
|
|
* @return {Promise} The result of calling `p.then(null, onFailure)`
|
|
* @see R.andThen
|
|
* @example
|
|
*
|
|
* const failedFetch = id => Promise.reject('bad ID');
|
|
* const useDefault = () => ({ firstName: 'Bob', lastName: 'Loblaw' });
|
|
*
|
|
* //recoverFromFailure :: String -> Promise ({ firstName, lastName })
|
|
* const recoverFromFailure = R.pipe(
|
|
* failedFetch,
|
|
* R.otherwise(useDefault),
|
|
* R.andThen(R.pick(['firstName', 'lastName'])),
|
|
* );
|
|
* recoverFromFailure(12345).then(console.log);
|
|
*/
|
|
var otherwise = _curry2(function otherwise(f, p) {
|
|
_assertPromise('otherwise', p);
|
|
|
|
return p.then(null, f);
|
|
});
|
|
export default otherwise;
|