Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

index.js 699B

1234567891011121314151617181920212223
  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. fn.apply(
  8. this,
  9. args.concat([(err, res) => err ? reject(err) : resolve(res)])
  10. )
  11. })
  12. }
  13. }, 'name', { value: fn.name })
  14. }
  15. exports.fromPromise = function (fn) {
  16. return Object.defineProperty(function (...args) {
  17. const cb = args[args.length - 1]
  18. if (typeof cb !== 'function') return fn.apply(this, args)
  19. else fn.apply(this, args.slice(0, -1)).then(r => cb(null, r), cb)
  20. }, 'name', { value: fn.name })
  21. }