@gar/promisify
Advanced tools
Comparing version 1.0.1 to 1.1.0
13
index.js
'use strict' | ||
const { promisify } = require('util') | ||
const handler = { | ||
@@ -8,2 +10,5 @@ get: function (target, prop, receiver) { | ||
} | ||
if (target[prop][promisify.custom]) { | ||
return target[prop][promisify.custom] | ||
} | ||
return function () { | ||
@@ -23,3 +28,9 @@ return new Promise((resolve, reject) => { | ||
module.exports = function (thingToPromisify) { | ||
return new Proxy(thingToPromisify, handler) | ||
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') | ||
} |
{ | ||
"name": "@gar/promisify", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Promisify an entire class or object", | ||
@@ -13,4 +13,8 @@ "main": "index.js", | ||
"lint:fix": "standard --fix", | ||
"test": "lab -a @hapi/code -L -t 100" | ||
"test": "lab -a @hapi/code -t 100", | ||
"posttest": "npm run lint" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
@@ -26,10 +30,5 @@ "promisify", | ||
"@hapi/code": "^8.0.1", | ||
"@hapi/lab": "^22.0.4", | ||
"eslint": "^7.1.0", | ||
"eslint-config-standard": "^14.1.1", | ||
"eslint-plugin-import": "^2.20.2", | ||
"eslint-plugin-node": "^11.1.0", | ||
"eslint-plugin-promise": "^4.2.1", | ||
"eslint-plugin-standard": "^4.0.1" | ||
"@hapi/lab": "^24.1.0", | ||
"standard": "^16.0.3" | ||
} | ||
} |
@@ -10,6 +10,17 @@ # @gar/promisify | ||
parameter, and that it is an error-first callback with only one value, | ||
i.e. `(err, value) => ...`. This mirrors node's util.proimisify method. | ||
i.e. `(err, value) => ...`. This mirrors node's `util.promisify` method. | ||
In order that you can use it as a one-stop-shop for all your promisify | ||
needs, you can also pass it a function. That function will be | ||
promisified as normal using node's built-in `util.promisify` method. | ||
[node's custom promisified | ||
functions](https://nodejs.org/api/util.html#util_custom_promisified_functions) | ||
will also be mirrored, further allowing this to be a drop-in replacement | ||
for the built-in `util.promisify`. | ||
### Examples | ||
Promisify an entire object | ||
```javascript | ||
@@ -34,1 +45,23 @@ | ||
``` | ||
Promisify a function | ||
```javascript | ||
const promisify = require('@gar/promisify') | ||
function foo (a, cb) { | ||
if (a !== 'bad') { | ||
return cb(null, 'ok') | ||
} | ||
return cb('not ok') | ||
} | ||
const promisified = promisify(foo) | ||
// This will resolve to 'ok' | ||
promisified('good') | ||
// this will reject | ||
promisified('bad') | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3
66
3018
3
31