Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/@gar/promisify/index.js')
-rw-r--r--node_modules/@gar/promisify/index.js36
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')
+}