try-to-catch.js 387 B

123456789101112131415161718192021
  1. 'use strict';
  2. const success = (a) => [null, a];
  3. const fail = (a) => [a];
  4. const noArg = (f, a) => () => f(...a);
  5. module.exports = (fn, ...args) => {
  6. check(fn);
  7. return Promise.resolve()
  8. .then(noArg(fn, args))
  9. .then(success)
  10. .catch(fail);
  11. };
  12. function check(fn) {
  13. if (typeof fn !== 'function')
  14. throw Error('fn should be a function!');
  15. }