otherwise.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. var otherwise =
  32. /*#__PURE__*/
  33. _curry2(function otherwise(f, p) {
  34. _assertPromise('otherwise', p);
  35. return p.then(null, f);
  36. });
  37. export default otherwise;