+42
-12
@@ -7,18 +7,28 @@ | ||
| /** | ||
| * Turn async functions into promises | ||
| * | ||
| * @param {Function} $$__fn__$$ | ||
| * @return {Function} | ||
| * @api public | ||
| */ | ||
| function thenify($$__fn__$$) { | ||
| assert(typeof $$__fn__$$ === 'function') | ||
| var name = $$__fn__$$.name || '' | ||
| return eval('(function ' + name + '() {\n' | ||
| + 'var self = this\n' | ||
| + 'var len = arguments.length\n' | ||
| + 'var args = new Array(len + 1)\n' | ||
| + 'for (var i = 0; i < len; ++i) args[i] = arguments[i]\n' | ||
| + 'var lastIndex = i\n' | ||
| + 'return new Promise(function (resolve, reject) {\n' | ||
| + 'args[lastIndex] = createCallback(resolve, reject)\n' | ||
| + '$$__fn__$$.apply(self, args)\n' | ||
| + '})\n' | ||
| + '})') | ||
| return eval(createWraper($$__fn__$$.name)) | ||
| } | ||
| /** | ||
| * Turn async functions into promises and backward compatible with callback | ||
| * | ||
| * @param {Function} $$__fn__$$ | ||
| * @return {Function} | ||
| * @api public | ||
| */ | ||
| thenify.withCallback = function ($$__fn__$$) { | ||
| assert(typeof $$__fn__$$ === 'function') | ||
| return eval(createWraper($$__fn__$$.name, true)) | ||
| } | ||
| function createCallback(resolve, reject) { | ||
@@ -34,1 +44,21 @@ return function(err, value) { | ||
| } | ||
| function createWraper(name, withCallback) { | ||
| withCallback = withCallback ? | ||
| 'var lastType = typeof arguments[len - 1]\n' | ||
| + 'if (lastType === "function") return $$__fn__$$.apply(self, arguments)\n' | ||
| : '' | ||
| return '(function ' + (name || '') + '() {\n' | ||
| + 'var self = this\n' | ||
| + 'var len = arguments.length\n' | ||
| + withCallback | ||
| + 'var args = new Array(len + 1)\n' | ||
| + 'for (var i = 0; i < len; ++i) args[i] = arguments[i]\n' | ||
| + 'var lastIndex = i\n' | ||
| + 'return new Promise(function (resolve, reject) {\n' | ||
| + 'args[lastIndex] = createCallback(resolve, reject)\n' | ||
| + '$$__fn__$$.apply(self, args)\n' | ||
| + '})\n' | ||
| + '})' | ||
| } |
+1
-1
| { | ||
| "name": "thenify", | ||
| "description": "Promisify a callback-based function", | ||
| "version": "3.0.0", | ||
| "version": "3.1.0", | ||
| "author": "Jonathan Ong <me@jongleberry.com> (http://jongleberry.com)", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
+16
-0
@@ -18,2 +18,3 @@ | ||
| - Resulting function never deoptimizes | ||
| - Supports both callback and promise style | ||
@@ -24,2 +25,4 @@ An added benefit is that `throw`n errors in that async function will be caught by the promise! | ||
| - Turn async functions into promises | ||
| ```js | ||
@@ -33,2 +36,15 @@ var thenify = require('thenify'); | ||
| - Backward compatible with callback | ||
| ```js | ||
| var thenify = require('thenify').withcallback; | ||
| var somethingAsync = thenify(function somethingAsync(a, b, c, callback) { | ||
| callback(null, a, b, c); | ||
| }); | ||
| // somethingAsync(a, b, c).then(onFulfilled).catch(onRejected); | ||
| // somethingAsync(a, b, c, function () {}); | ||
| ``` | ||
| ### var fn = thenify(fn) | ||
@@ -35,0 +51,0 @@ |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
5887
21.93%53
89.29%69
30.19%3
200%