es6-promisify
Advanced tools
Comparing version 5.0.0 to 6.0.0
"use strict"; | ||
/* global module, require */ | ||
module.exports = function () { | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
// Symbols is a better way to do this, but if we don't have support we'll just | ||
// have to make do with an unlikely token | ||
var customArgumentsToken = Symbol ? Symbol("__ES6-PROMISIFY--CUSTOM-ARGUMENTS__") : "__ES6-PROMISIFY--CUSTOM-ARGUMENTS__"; | ||
"use strict"; | ||
/** | ||
* promisify() | ||
* Transforms callback-based function -- func(arg1, arg2 .. argN, callback) -- into | ||
* an ES6-compatible Promise. Promisify provides a default callback of the form (error, result) | ||
* and rejects when `error` is truthy. | ||
* | ||
* @param {function} original - The function to promisify | ||
* @return {function} A promisified version of `original` | ||
*/ | ||
function promisify(original) { | ||
// Get a promise object. This may be native, or it may be polyfilled | ||
// Ensure the argument is a function | ||
if (typeof original !== "function") { | ||
throw new TypeError("Argument to promisify must be a function"); | ||
} | ||
var ES6Promise = require("./promise.js"); | ||
// If the user has asked us to decode argument names for them, honour that | ||
var argumentNames = original[customArgumentsToken]; | ||
/** | ||
* thatLooksLikeAPromiseToMe() | ||
* | ||
* Duck-types a promise. | ||
* | ||
* @param {object} o | ||
* @return {bool} True if this resembles a promise | ||
*/ | ||
function thatLooksLikeAPromiseToMe(o) { | ||
return o && typeof o.then === "function" && typeof o.catch === "function"; | ||
// If the user has supplied a custom Promise implementation, use it. Otherwise | ||
// fall back to whatever we can find on the global object. | ||
var ES6Promise = promisify.Promise || Promise; | ||
// If we can find no Promise implemention, then fail now. | ||
if (typeof ES6Promise !== "function") { | ||
throw new Error("No Promise implementation found; do you need a polyfill?"); | ||
} | ||
/** | ||
* promisify() | ||
* | ||
* Transforms callback-based function -- func(arg1, arg2 .. argN, callback) -- into | ||
* an ES6-compatible Promise. Promisify provides a default callback of the form (error, result) | ||
* and rejects when `error` is truthy. You can also supply settings object as the second argument. | ||
* | ||
* @param {function} original - The function to promisify | ||
* @param {object} settings - Settings object | ||
* @param {object} settings.thisArg - A `this` context to use. If not set, assume `settings` _is_ `thisArg` | ||
* @param {bool} settings.multiArgs - Should multiple arguments be returned as an array? | ||
* @return {function} A promisified version of `original` | ||
*/ | ||
return function promisify(original, settings) { | ||
return function () { | ||
var _this = this; | ||
return function () { | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
var returnMultipleArguments = settings && settings.multiArgs; | ||
return new ES6Promise(function (resolve, reject) { | ||
var target = void 0; | ||
if (settings && settings.thisArg) { | ||
target = settings.thisArg; | ||
} else if (settings) { | ||
target = settings; | ||
} | ||
// Append the callback bound to the context | ||
args.push(function callback(err) { | ||
// Return the promisified function | ||
return new ES6Promise(function (resolve, reject) { | ||
if (err) { | ||
return reject(err); | ||
} | ||
// Append the callback bound to the context | ||
args.push(function callback(err) { | ||
for (var _len2 = arguments.length, values = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { | ||
values[_key2 - 1] = arguments[_key2]; | ||
} | ||
if (err) { | ||
return reject(err); | ||
} | ||
if (values.length === 1 || !argumentNames) { | ||
return resolve(values[0]); | ||
} | ||
for (var _len2 = arguments.length, values = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { | ||
values[_key2 - 1] = arguments[_key2]; | ||
var o = {}; | ||
values.forEach(function (value, index) { | ||
var name = argumentNames[index]; | ||
if (name) { | ||
o[name] = value; | ||
} | ||
}); | ||
if (false === !!returnMultipleArguments) { | ||
return resolve(values[0]); | ||
} | ||
resolve(o); | ||
}); | ||
resolve(values); | ||
}); | ||
// Call the function. | ||
original.call.apply(original, [_this].concat(args)); | ||
}); | ||
}; | ||
} | ||
// Call the function | ||
var response = original.apply(target, args); | ||
// Attach this symbol to the exported function, so users can use it | ||
promisify.argumentNames = customArgumentsToken; | ||
promisify.Promise = undefined; | ||
// If it looks like original already returns a promise, | ||
// then just resolve with that promise. Hopefully, the callback function we added will just be ignored. | ||
if (thatLooksLikeAPromiseToMe(response)) { | ||
resolve(response); | ||
} | ||
}); | ||
}; | ||
}; | ||
}(); | ||
// Export the public API | ||
exports.promisify = promisify; |
{ | ||
"name": "es6-promisify", | ||
"version": "5.0.0", | ||
"version": "6.0.0", | ||
"description": "Converts callback-based functions to ES6 Promises", | ||
@@ -13,13 +13,12 @@ "main": "dist/promisify.js", | ||
"license": "MIT", | ||
"dependencies": { | ||
"es6-promise": "^4.0.3" | ||
}, | ||
"dependencies": {}, | ||
"scripts": { | ||
"pretest": "./node_modules/eslint/bin/eslint.js ./lib/*.js ./tests/*.js", | ||
"test": "gulp && nodeunit tests" | ||
"pretest": "./node_modules/.bin/eslint lib/*.js test/*.js", | ||
"build": "./node_modules/.bin/babel lib -d dist", | ||
"test": "npm run build && ./node_modules/.bin/tape test", | ||
"test:cover": "npm run build && ./node_modules/.bin/istanbul cover test" | ||
}, | ||
"bugs": "http://github.com/digitaldesignlabs/es6-promisify/issues", | ||
"files": [ | ||
"dist/promisify.js", | ||
"dist/promise.js" | ||
"dist/promisify.js" | ||
], | ||
@@ -31,7 +30,9 @@ "repository": { | ||
"devDependencies": { | ||
"babel-preset-es2015": "^6.9.0", | ||
"eslint": "^2.13.1", | ||
"gulp": "^3.9.1", | ||
"gulp-babel": "^6.1.2", | ||
"nodeunit": "^0.10.0" | ||
"babel-cli": "^6.26.0", | ||
"babel-preset-es2015": "^6.24.1", | ||
"es6-promise": "^4.1.1", | ||
"eslint": "^4.9.0", | ||
"istanbul": "^0.4.5", | ||
"sinon": "^4.0.1", | ||
"tape": "^4.8.0" | ||
}, | ||
@@ -38,0 +39,0 @@ "greenkeeper": { |
[![Travis CI](https://travis-ci.org/digitaldesignlabs/es6-promisify.svg)](https://travis-ci.org/digitaldesignlabs/es6-promisify) | ||
# es6-promisify | ||
Converts callback-based functions to ES6/ES2015 Promises, using a boilerplate callback function. | ||
Converts callback-based functions to Promise-based functions. | ||
NOTE: All-new API for Version 6.0.0; please read carefully! | ||
=========================================================== | ||
## Install | ||
Install with [npm](https://npmjs.org/package/es6-promisify) | ||
```bash | ||
npm install --save es6-promisify | ||
npm install es6-promisify | ||
``` | ||
## Example | ||
```js | ||
"use strict"; | ||
const {promisify} = require("es6-promisify"); | ||
// Declare variables | ||
const promisify = require("es6-promisify"); | ||
// Convert the stat function | ||
const fs = require("fs"); | ||
// Convert the stat function | ||
const stat = promisify(fs.stat); | ||
@@ -37,11 +34,8 @@ | ||
```js | ||
"use strict"; | ||
const {promisify} = require("es6-promisify"); | ||
// Declare variables | ||
const promisify = require("es6-promisify"); | ||
// Create a promise-based version of send_command | ||
const redis = require("redis").createClient(6379, "localhost"); | ||
const client = promisify(redis.send_command.bind(redis)); | ||
// Create a promise-based version of send_command | ||
const client = promisify(redis.send_command, redis); | ||
// Send commands to redis and get a promise back | ||
@@ -57,7 +51,6 @@ client("ping").then(function (pong) { | ||
## Handle callback multiple arguments | ||
## Handle multiple callback arguments, with named parameters | ||
```js | ||
"use strict"; | ||
const {promisify} = require("es6-promisify"); | ||
// Declare functions | ||
function test(cb) { | ||
@@ -67,17 +60,22 @@ return cb(undefined, 1, 2, 3); | ||
// Declare variables | ||
const promisify = require("es6-promisify"); | ||
// Create promise-based version of test | ||
const single = promisify(test); | ||
const multi = promisify(test, {multiArgs: true}); | ||
test[promisify.argumentNames] = ["one", "two", "three"]; | ||
const multi = promisify(test); | ||
// Discards additional arguments | ||
single().then(function (result) { | ||
console.log(result); // 1 | ||
// Returns named arguments | ||
multi().then(result => { | ||
console.log(result); // {one: 1, two: 2, three: 3} | ||
}); | ||
``` | ||
// Returns all arguments as an array | ||
multi().then(function (result) { | ||
console.log(result); // [1, 2, 3] | ||
## Provide your own Promise implementation | ||
```js | ||
const {promisify} = require("es6-promisify"); | ||
// Now uses Bluebird | ||
promisify.Promise = require("bluebird"); | ||
const test = promisify(cb => cb(undefined, "test")); | ||
test().then(result => { | ||
console.log(result); // "test", resolved using Bluebird | ||
}); | ||
@@ -87,3 +85,3 @@ ``` | ||
### Tests | ||
Test with nodeunit | ||
Test with tape | ||
```bash | ||
@@ -93,2 +91,8 @@ $ npm test | ||
### Changes from v5.0.0 | ||
- Allow developer to specify a different implementations of `Promise` | ||
- No longer ships with a polyfill for `Promise`. If your environment has no native `Promise` you must polyfill yourself, or set `promisify.Promise` to an A+ compatible `Promise` implementation. | ||
- Removed support for `settings.thisArg`: use `.bind()` instead. | ||
- Removed support for `settings.multiArgs`: use named arguments instead. | ||
Published under the [MIT License](http://opensource.org/licenses/MIT). |
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
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
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
0
94
7542
7
65
1
- Removedes6-promise@^4.0.3
- Removedes6-promise@4.2.8(transitive)