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.

25 lines
777 B

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