Comparing version 1.0.0 to 2.0.0
53
index.js
Array.prototype.then = function (resolve, reject) { | ||
// unfortunately, then can only be used once :( | ||
unthenify(this) | ||
var Promise = require('native-or-bluebird') | ||
var length = this.length | ||
var hasPromise = false | ||
for (var i = 0; i < length; i++) { | ||
var val = this[i] | ||
if (val && typeof val.then === 'function') { | ||
hasPromise = true | ||
break | ||
} | ||
module.exports = thenify | ||
function thenify(name, $$__fn__$$) { | ||
if (typeof name === 'function') { | ||
$$__fn__$$ = name | ||
name = $$__fn__$$.name || 'anonymous' | ||
} | ||
if (!hasPromise) return Promise.resolve(this).then(resolve, reject) | ||
return Promise.all(this).then(resolve, reject) | ||
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' | ||
+ '})') | ||
} | ||
Array.prototype.catch = function (reject) { | ||
return this.then(null, reject) | ||
function createCallback(resolve, reject) { | ||
return function(err, value) { | ||
if (err) return reject(err) | ||
var length = arguments.length | ||
if (length <= 2) return resolve(value) | ||
var values = new Array(length - 1) | ||
for (var i = 1; i < length; ++i) values[i - 1] = arguments[i] | ||
resolve(values) | ||
} | ||
} | ||
function unthenify(obj) { | ||
Object.defineProperty(obj, 'then', { | ||
value: undefined, | ||
enumerable: false, | ||
configurable: true, | ||
writable: true, | ||
}) | ||
return obj | ||
} |
{ | ||
"name": "thenify", | ||
"description": "Globally make everything a promise!", | ||
"version": "1.0.0", | ||
"author": { | ||
"name": "Jonathan Ong", | ||
"email": "me@jongleberry.com", | ||
"url": "http://jongleberry.com", | ||
"twitter": "https://twitter.com/jongleberry" | ||
}, | ||
"description": "Promisify a callback-based function", | ||
"version": "2.0.0", | ||
"author": "Jonathan Ong <me@jongleberry.com> (http://jongleberry.com)", | ||
"license": "MIT", | ||
"repository": "thenables/thenify", | ||
"dependencies": {}, | ||
"dependencies": { | ||
"native-or-bluebird": "1" | ||
}, | ||
"devDependencies": { | ||
"mocha": "1", | ||
"istanbul": "0" | ||
"bluebird": "2", | ||
"istanbul": "0", | ||
"mocha": "2" | ||
}, | ||
@@ -24,9 +22,11 @@ "scripts": { | ||
"keywords": [ | ||
"promisify", | ||
"promise", | ||
"thenify", | ||
"then", | ||
"array", | ||
"promise", | ||
"promises", | ||
"await" | ||
"es6" | ||
], | ||
"files": ["index.js"] | ||
"files": [ | ||
"index.js" | ||
] | ||
} |
@@ -5,68 +5,51 @@ | ||
[![NPM version][npm-image]][npm-url] | ||
[![build status][travis-image]][travis-url] | ||
[![Build status][travis-image]][travis-url] | ||
[![Test coverage][coveralls-image]][coveralls-url] | ||
[![Dependency Status][david-image]][david-url] | ||
[![License][license-image]][license-url] | ||
[![Downloads][downloads-image]][downloads-url] | ||
[![Gittip][gittip-image]][gittip-url] | ||
Globally make everything a promise! | ||
Promisify a callback-based function. | ||
```js | ||
Promise.resolve([ | ||
Promise.resolve(0), | ||
Promise.resolve(1) | ||
]).then(function (results) { | ||
assert.equal(0, results[0]) | ||
assert.equal(1, results[1]) | ||
}) | ||
``` | ||
- Preserves function names | ||
- Uses a native promise implementation if available and tries to fall back to `bluebird` | ||
- Converts multiple arguments from the callback into an `Array` | ||
- Resulting function never deoptimizes | ||
> DO NOT USE THIS IN LIBRARIES! THIS ADDS METHODS TO THE NATIVE PROTOTYPES AND | ||
> IS CONSIDERED TERRORISM! ONLY USE THIS IN YOUR APP AND TEST THOROUGHLY! | ||
An added benefit is that `throw`n errors in that async function will be caught by the promise! | ||
## Context | ||
## API | ||
The current [async/await](https://github.com/lukehoban/ecmascript-asyncawait) proposal | ||
does not support `await []`, which is [co's](https://github.com/visionmedia/co) | ||
signature to support yielding multiple asynchronous functions in parallel. | ||
There is very little chance of having this overloading being included in the specifications. | ||
However, [@domenic](https://twitter.com/domenic) had a crazy idea: | ||
[globally make arrays a promise!](https://twitter.com/domenic/status/494949612088594432). | ||
This is what this repository does. | ||
```js | ||
async function () { | ||
var results = await [ | ||
true, | ||
Promise.resolve(1) | ||
]; | ||
var thenify = require('thenify'); | ||
// results[0] === true | ||
// results[1] === 1 | ||
} | ||
var somethingAsync = thenify(function somethingAsync(a, b, c, callback) { | ||
callback(null, a, b, c); | ||
}); | ||
``` | ||
## Usage | ||
### var fn = thenify([name], fn) | ||
Just somehow get [index.js](index.js) included in your app. | ||
Promisifies a function. | ||
Optionally set a custom name to the function, | ||
defaulting to `fn.name`. | ||
Node: | ||
```js | ||
require('thenify') | ||
``` | ||
HTML: | ||
```js | ||
<script src="https://cdn.rawgit.com/thenables/thenify/master/index.js"></script> | ||
``` | ||
[npm-image]: https://img.shields.io/npm/v/thenify.svg?style=flat | ||
[gitter-image]: https://badges.gitter.im/thenables/thenify.png | ||
[gitter-url]: https://gitter.im/thenables/thenify | ||
[npm-image]: https://img.shields.io/npm/v/thenify.svg?style=flat-square | ||
[npm-url]: https://npmjs.org/package/thenify | ||
[travis-image]: https://img.shields.io/travis/thenables/thenify.svg?style=flat | ||
[github-tag]: http://img.shields.io/github/tag/thenables/thenify.svg?style=flat-square | ||
[github-url]: https://github.com/thenables/thenify/tags | ||
[travis-image]: https://img.shields.io/travis/thenables/thenify.svg?style=flat-square | ||
[travis-url]: https://travis-ci.org/thenables/thenify | ||
[coveralls-image]: https://img.shields.io/coveralls/thenables/thenify.svg?style=flat | ||
[coveralls-url]: https://coveralls.io/r/thenables/thenify?branch=master | ||
[gittip-image]: https://img.shields.io/gittip/jonathanong.svg?style=flat | ||
[gittip-url]: https://www.gittip.com/jonathanong/ | ||
[coveralls-image]: https://img.shields.io/coveralls/thenables/thenify.svg?style=flat-square | ||
[coveralls-url]: https://coveralls.io/r/thenables/thenify | ||
[david-image]: http://img.shields.io/david/thenables/thenify.svg?style=flat-square | ||
[david-url]: https://david-dm.org/thenables/thenify | ||
[license-image]: http://img.shields.io/npm/l/thenify.svg?style=flat-square | ||
[license-url]: LICENSE | ||
[downloads-image]: http://img.shields.io/npm/dm/thenify.svg?style=flat-square | ||
[downloads-url]: https://npmjs.org/package/thenify | ||
[gittip-image]: https://img.shields.io/gratipay/jonathanong.svg?style=flat-square | ||
[gittip-url]: https://gratipay.com/jonathanong/ |
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
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
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
4908
4
29
1
3
55
1
+ Addednative-or-bluebird@1
+ Addednative-or-bluebird@1.2.0(transitive)