callbackify.js 372 B

12345678910111213141516
  1. import utils from "../utils.js";
  2. const callbackify = (fn, reducer) => {
  3. return utils.isAsyncFn(fn) ? function (...args) {
  4. const cb = args.pop();
  5. fn.apply(this, args).then((value) => {
  6. try {
  7. reducer ? cb(null, ...reducer(value)) : cb(null, value);
  8. } catch (err) {
  9. cb(err);
  10. }
  11. }, cb);
  12. } : fn;
  13. }
  14. export default callbackify;