UNPKG

706 BJavaScriptView Raw
1'use strict'
2
3exports.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 args.push((err, res) => (err != null) ? reject(err) : resolve(res))
9 fn.apply(this, args)
10 })
11 }
12 }, 'name', { value: fn.name })
13}
14
15exports.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 {
20 args.pop()
21 fn.apply(this, args).then(r => cb(null, r), cb)
22 }
23 }, 'name', { value: fn.name })
24}