Socket
Socket
Sign inDemoInstall

thenify

Package Overview
Dependencies
Maintainers
3
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

thenify - npm Package Compare versions

Comparing version 3.2.1 to 3.3.0

History.md

35

index.js

@@ -15,5 +15,5 @@

function thenify($$__fn__$$) {
function thenify($$__fn__$$, options) {
assert(typeof $$__fn__$$ === 'function')
return eval(createWrapper($$__fn__$$.name))
return eval(createWrapper($$__fn__$$.name, options))
}

@@ -29,12 +29,23 @@

thenify.withCallback = function ($$__fn__$$) {
thenify.withCallback = function ($$__fn__$$, options) {
assert(typeof $$__fn__$$ === 'function')
return eval(createWrapper($$__fn__$$.name, true))
options = options || {}
options.withCallback = true
if (options.multiArgs === undefined) options.multiArgs = true
return eval(createWrapper($$__fn__$$.name, options))
}
function createCallback(resolve, reject) {
function createCallback(resolve, reject, multiArgs) {
return function(err, value) {
if (err) return reject(err)
var length = arguments.length
if (length <= 2) return resolve(value)
if (length <= 2 || !multiArgs) return resolve(value)
if (Array.isArray(multiArgs)) {
var values = {}
for (var i = 1; i < length; i++) values[multiArgs[i - 1]] = arguments[i]
return resolve(values)
}
var values = new Array(length - 1)

@@ -46,5 +57,10 @@ for (var i = 1; i < length; ++i) values[i - 1] = arguments[i]

function createWrapper(name, withCallback) {
function createWrapper(name, options) {
name = (name || '').replace(/\s|bound(?!$)/g, '')
withCallback = withCallback ?
options = options || {}
// default to true
var multiArgs = options.multiArgs !== undefined ? options.multiArgs : true
multiArgs = 'var multiArgs = ' + JSON.stringify(multiArgs) + '\n'
var withCallback = options.withCallback ?
'var lastType = typeof arguments[len - 1]\n'

@@ -57,2 +73,3 @@ + 'if (lastType === "function") return $$__fn__$$.apply(self, arguments)\n'

+ 'var len = arguments.length\n'
+ multiArgs
+ withCallback

@@ -63,3 +80,3 @@ + 'var args = new Array(len + 1)\n'

+ 'return new Promise(function (resolve, reject) {\n'
+ 'args[lastIndex] = createCallback(resolve, reject)\n'
+ 'args[lastIndex] = createCallback(resolve, reject, multiArgs)\n'
+ '$$__fn__$$.apply(self, args)\n'

@@ -66,0 +83,0 @@ + '})\n'

{
"name": "thenify",
"description": "Promisify a callback-based function",
"version": "3.2.1",
"version": "3.3.0",
"author": "Jonathan Ong <me@jongleberry.com> (http://jongleberry.com)",

@@ -6,0 +6,0 @@ "license": "MIT",

@@ -15,3 +15,3 @@

- Uses a native promise implementation if available and tries to fall back to a promise implementation such as `bluebird`
- Converts multiple arguments from the callback into an `Array`
- Converts multiple arguments from the callback into an `Array`, also support change the behavior by `options.multiArgs`
- Resulting function never deoptimizes

@@ -24,2 +24,16 @@ - Supports both callback and promise style

### fn = thenify(fn, options)
Promisifies a function.
### Options
`options` are optional.
- `options.withCallback` - support both callback and promise style, default to `false`.
- `options.multiArgs` - change the behavior when callback have multiple arguments. default to `true`.
- `true` - converts multiple arguments to an array
- `false`- always use the first argument
- `Array` - converts multiple arguments to an object with keys provided in `options.multiArgs`
- Turn async functions into promises

@@ -38,2 +52,15 @@

```js
var thenify = require('thenify');
var somethingAsync = thenify(function somethingAsync(a, b, c, callback) {
callback(null, a, b, c);
}, { withCallback: true });
// somethingAsync(a, b, c).then(onFulfilled).catch(onRejected);
// somethingAsync(a, b, c, function () {});
```
or use `thenify.withCallback()`
```js
var thenify = require('thenify').withCallback;

@@ -49,6 +76,34 @@

### var fn = thenify(fn)
- Always return the first argument in callback
Promisifies a function.
```js
var thenify = require('thenify');
var promise = thenify(function (callback) {
callback(null, 1, 2, 3);
}, { multiArgs: false });
// promise().then(function onFulfilled(value) {
// assert.equal(value, 1);
// });
```
- Converts callback arguments to an object
```js
var thenify = require('thenify');
var promise = thenify(function (callback) {
callback(null, 1, 2, 3);
}, { multiArgs: [ 'one', 'tow', 'three' ] });
// promise().then(function onFulfilled(value) {
// assert.deepEqual(value, {
// one: 1,
// tow: 2,
// three: 3
// });
// });
```
[gitter-image]: https://badges.gitter.im/thenables/thenify.png

@@ -55,0 +110,0 @@ [gitter-url]: https://gitter.im/thenables/thenify

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc