index.js 706 B

123456789101112131415161718192021222324
  1. 'use strict'
  2. exports.fromCallback = function (fn) {
  3. return Object.defineProperty(function (...args) {
  4. if (typeof args[args.length - 1] === 'function') fn.apply(this, args)
  5. else {
  6. return new Promise((resolve, reject) => {
  7. args.push((err, res) => (err != null) ? reject(err) : resolve(res))
  8. fn.apply(this, args)
  9. })
  10. }
  11. }, 'name', { value: fn.name })
  12. }
  13. exports.fromPromise = function (fn) {
  14. return Object.defineProperty(function (...args) {
  15. const cb = args[args.length - 1]
  16. if (typeof cb !== 'function') return fn.apply(this, args)
  17. else {
  18. args.pop()
  19. fn.apply(this, args).then(r => cb(null, r), cb)
  20. }
  21. }, 'name', { value: fn.name })
  22. }