ghaction-github-pages/node_modules/universalify/index.js

24 lines
699 B
JavaScript
Raw Normal View History

2019-11-15 23:20:34 +08:00
'use strict'
exports.fromCallback = function (fn) {
2020-03-23 17:00:06 +08:00
return Object.defineProperty(function (...args) {
if (typeof args[args.length - 1] === 'function') fn.apply(this, args)
2019-11-15 23:20:34 +08:00
else {
return new Promise((resolve, reject) => {
2020-03-23 17:00:06 +08:00
fn.apply(
this,
args.concat([(err, res) => err ? reject(err) : resolve(res)])
)
2019-11-15 23:20:34 +08:00
})
}
}, 'name', { value: fn.name })
}
exports.fromPromise = function (fn) {
2020-03-23 17:00:06 +08:00
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)
2019-11-15 23:20:34 +08:00
}, 'name', { value: fn.name })
}