bf8581b7e7c06a1718b3a36d1e47cd045318515256eb3691e8b3958c2d54290646acb1c8299f7e480be51f1ee7f0717a1a013103e0e38209d5d2742259828f 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. }