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