attemptify.js 788 B

1234567891011121314151617181920212223242526
  1. "use strict";
  2. /* IMPORT */
  3. Object.defineProperty(exports, "__esModule", { value: true });
  4. exports.attemptifySync = exports.attemptifyAsync = void 0;
  5. const consts_1 = require("../consts");
  6. /* ATTEMPTIFY */
  7. //TODO: Maybe publish this as a standalone package
  8. //FIXME: The type castings here aren't exactly correct
  9. const attemptifyAsync = (fn, onError = consts_1.NOOP) => {
  10. return function () {
  11. return fn.apply(undefined, arguments).catch(onError);
  12. };
  13. };
  14. exports.attemptifyAsync = attemptifyAsync;
  15. const attemptifySync = (fn, onError = consts_1.NOOP) => {
  16. return function () {
  17. try {
  18. return fn.apply(undefined, arguments);
  19. }
  20. catch (error) {
  21. return onError(error);
  22. }
  23. };
  24. };
  25. exports.attemptifySync = attemptifySync;