andThen.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import _curry2 from "./internal/_curry2.js";
  2. import _assertPromise from "./internal/_assertPromise.js";
  3. /**
  4. * Returns the result of applying the onSuccess function to the value inside
  5. * a successfully resolved promise. This is useful for working with promises
  6. * inside function compositions.
  7. *
  8. * @func
  9. * @memberOf R
  10. * @since v0.27.1
  11. * @category Function
  12. * @sig (a -> b) -> (Promise e a) -> (Promise e b)
  13. * @sig (a -> (Promise e b)) -> (Promise e a) -> (Promise e b)
  14. * @param {Function} onSuccess 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(onSuccess)`
  17. * @see R.otherwise
  18. * @example
  19. *
  20. * const makeQuery = email => ({ query: { email }});
  21. * const fetchMember = request =>
  22. * Promise.resolve({ firstName: 'Bob', lastName: 'Loblaw', id: 42 });
  23. *
  24. * //getMemberName :: String -> Promise ({ firstName, lastName })
  25. * const getMemberName = R.pipe(
  26. * makeQuery,
  27. * fetchMember,
  28. * R.andThen(R.pick(['firstName', 'lastName']))
  29. * );
  30. *
  31. * getMemberName('bob@gmail.com').then(console.log);
  32. */
  33. var andThen =
  34. /*#__PURE__*/
  35. _curry2(function andThen(f, p) {
  36. _assertPromise('andThen', p);
  37. return p.then(f);
  38. });
  39. export default andThen;