diff options
author | Gar <gar+gh@danger.computer> | 2021-08-26 21:20:59 +0300 |
---|---|---|
committer | Gar <gar+gh@danger.computer> | 2021-08-26 21:20:59 +0300 |
commit | e63a942c685233fa546788981ed9c144220d50e1 (patch) | |
tree | 4115e9ae913ad1b688c35711418a528184c9b337 /node_modules/@gar/promisify/index.js | |
parent | 7af36bb9f8e5c9facaa8deb114b76368841fbc66 (diff) |
cacache@15.3.0
* feat: introduce @npmcli/fs for tmp dir methods
Diffstat (limited to 'node_modules/@gar/promisify/index.js')
-rw-r--r-- | node_modules/@gar/promisify/index.js | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/node_modules/@gar/promisify/index.js b/node_modules/@gar/promisify/index.js new file mode 100644 index 000000000..d0be95f6f --- /dev/null +++ b/node_modules/@gar/promisify/index.js @@ -0,0 +1,36 @@ +'use strict' + +const { promisify } = require('util') + +const handler = { + get: function (target, prop, receiver) { + if (typeof target[prop] !== 'function') { + return target[prop] + } + if (target[prop][promisify.custom]) { + return function () { + return Reflect.get(target, prop, receiver)[promisify.custom].apply(target, arguments) + } + } + return function () { + return new Promise((resolve, reject) => { + Reflect.get(target, prop, receiver).apply(target, [...arguments, function (err, result) { + if (err) { + return reject(err) + } + resolve(result) + }]) + }) + } + } +} + +module.exports = function (thingToPromisify) { + if (typeof thingToPromisify === 'function') { + return promisify(thingToPromisify) + } + if (typeof thingToPromisify === 'object') { + return new Proxy(thingToPromisify, handler) + } + throw new TypeError('Can only promisify functions or objects') +} |