Comparing version 1.1.1 to 2.0.0
17
index.js
'use strict'; | ||
var pify = module.exports = function (fn, P) { | ||
P = P || Promise; | ||
var pify = module.exports = function (fn, P, opts) { | ||
if (typeof P !== 'function') { | ||
opts = P; | ||
P = Promise; | ||
} | ||
opts = opts || {}; | ||
if (typeof fn !== 'function') { | ||
throw new TypeError('Expected a function'); | ||
return P.reject(new TypeError('Expected a function')); | ||
} | ||
@@ -18,3 +23,3 @@ | ||
reject(err); | ||
} else if (arguments.length > 2) { | ||
} else if (opts.multiArgs) { | ||
resolve([].slice.call(arguments, 1)); | ||
@@ -31,3 +36,3 @@ } else { | ||
pify.all = function (obj, P) { | ||
pify.all = function (obj, P, opts) { | ||
var ret = {}; | ||
@@ -37,3 +42,3 @@ | ||
var x = obj[key]; | ||
ret[key] = typeof x === 'function' ? pify(x, P) : x; | ||
ret[key] = typeof x === 'function' ? pify(x, P, opts) : x; | ||
}); | ||
@@ -40,0 +45,0 @@ |
{ | ||
"name": "pify", | ||
"version": "1.1.1", | ||
"version": "2.0.0", | ||
"description": "Promisify a callback-style function", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -36,8 +36,6 @@ # pify [![Build Status](https://travis-ci.org/sindresorhus/pify.svg?branch=master)](https://travis-ci.org/sindresorhus/pify) | ||
### pify(input, [promiseModule]) | ||
### pify(input, [promiseModule], [options]) | ||
Returns a promise wrapped version of the supplied function. | ||
If the callback of the supplied function gets more than two arguments the result will be an array. | ||
#### input | ||
@@ -49,3 +47,3 @@ | ||
### pify.all(module, [promiseModule]) | ||
### pify.all(module, [promiseModule], [options]) | ||
@@ -68,5 +66,23 @@ Returns a version of the module with all its methods promisified. | ||
#### options | ||
##### multiArgs | ||
Type: `boolean` | ||
Default: `false` | ||
By default, the promisified function will only return the second argument from the callback, which works fine for most APIs. This option can be useful for modules like `request` that return multiple arguments. Turning this on will make it return an array of all arguments from the callback, excluding the error argument, instead of just the second argument. | ||
```js | ||
const request = require('request'); | ||
const pify = require('pify'); | ||
pify(request, {multiArgs: true})('http://sindresorhus.com').then(result => { | ||
const [httpResponse, body] = result; | ||
}); | ||
``` | ||
## License | ||
MIT © [Sindre Sorhus](http://sindresorhus.com) |
4520
35
86