You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

24 lines
706 B

1 month ago
  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. }