maybe-async-result.js 496 B

1234567891011121314151617
  1. import { isPromise } from './is-promise.js';
  2. export function maybeAsyncResult(getResult, resultHandler, errorHandler = (err) => {
  3. throw err;
  4. }) {
  5. try {
  6. const result = isFunction(getResult) ? getResult() : getResult;
  7. return isPromise(result)
  8. ? result.then((result) => resultHandler(result))
  9. : resultHandler(result);
  10. }
  11. catch (err) {
  12. return errorHandler(err);
  13. }
  14. }
  15. function isFunction(arg) {
  16. return typeof arg === 'function';
  17. }