Urara-Blog/node_modules/.pnpm-store/v3/files/17/56aea1a704abb5b3646771dcb4524e542f37a8856d59bdbd0efeab41d5fd651124d4dd5067420e3ac9f56c16d3c8ec2f98674a1715eacd4c799c90cdc37794
2022-08-14 01:14:53 +08:00

24 lines
712 B
Text

'use strict'
exports.fromCallback = function (fn) {
return Object.defineProperty(function (...args) {
if (typeof args[args.length - 1] === 'function') fn.apply(this, args)
else {
return new Promise((resolve, reject) => {
fn.call(
this,
...args,
(err, res) => (err != null) ? reject(err) : resolve(res)
)
})
}
}, 'name', { value: fn.name })
}
exports.fromPromise = function (fn) {
return Object.defineProperty(function (...args) {
const cb = args[args.length - 1]
if (typeof cb !== 'function') return fn.apply(this, args)
else fn.apply(this, args.slice(0, -1)).then(r => cb(null, r), cb)
}, 'name', { value: fn.name })
}