Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

pify

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pify - npm Package Compare versions

Comparing version 2.3.0 to 3.0.0

102

index.js
'use strict';
var processFn = function (fn, P, opts) {
return function () {
var that = this;
var args = new Array(arguments.length);
const processFn = (fn, opts) => function () {
const P = opts.promiseModule;
const args = new Array(arguments.length);
for (var i = 0; i < arguments.length; i++) {
args[i] = arguments[i];
}
for (let i = 0; i < arguments.length; i++) {
args[i] = arguments[i];
}
return new P(function (resolve, reject) {
return new P((resolve, reject) => {
if (opts.errorFirst) {
args.push(function (err, result) {
if (err) {
reject(err);
} else if (opts.multiArgs) {
var results = new Array(arguments.length - 1);
if (opts.multiArgs) {
const results = new Array(arguments.length - 1);
for (var i = 1; i < arguments.length; i++) {
for (let i = 1; i < arguments.length; i++) {
results[i - 1] = arguments[i];
}
if (err) {
results.unshift(err);
reject(results);
} else {
resolve(results);
}
} else if (err) {
reject(err);
} else {
resolve(result);
}
});
} else {
args.push(function (result) {
if (opts.multiArgs) {
const results = new Array(arguments.length - 1);
for (let i = 0; i < arguments.length; i++) {
results[i] = arguments[i];
}
resolve(results);

@@ -28,42 +47,39 @@ } else {

});
}
fn.apply(that, args);
});
};
fn.apply(this, args);
});
};
var pify = module.exports = function (obj, P, opts) {
if (typeof P !== 'function') {
opts = P;
P = Promise;
}
module.exports = (obj, opts) => {
opts = Object.assign({
exclude: [/.+(Sync|Stream)$/],
errorFirst: true,
promiseModule: Promise
}, opts);
opts = opts || {};
opts.exclude = opts.exclude || [/.+Sync$/];
var filter = function (key) {
var match = function (pattern) {
return typeof pattern === 'string' ? key === pattern : pattern.test(key);
};
const filter = key => {
const match = pattern => typeof pattern === 'string' ? key === pattern : pattern.test(key);
return opts.include ? opts.include.some(match) : !opts.exclude.some(match);
};
var ret = typeof obj === 'function' ? function () {
if (opts.excludeMain) {
return obj.apply(this, arguments);
}
let ret;
if (typeof obj === 'function') {
ret = function () {
if (opts.excludeMain) {
return obj.apply(this, arguments);
}
return processFn(obj, P, opts).apply(this, arguments);
} : {};
return processFn(obj, opts).apply(this, arguments);
};
} else {
ret = Object.create(Object.getPrototypeOf(obj));
}
return Object.keys(obj).reduce(function (ret, key) {
var x = obj[key];
for (const key in obj) { // eslint-disable-line guard-for-in
const x = obj[key];
ret[key] = typeof x === 'function' && filter(key) ? processFn(x, opts) : x;
}
ret[key] = typeof x === 'function' && filter(key) ? processFn(x, P, opts) : x;
return ret;
}, ret);
return ret;
};
pify.all = pify;
{
"name": "pify",
"version": "2.3.0",
"version": "3.0.0",
"description": "Promisify a callback-style function",

@@ -13,3 +13,3 @@ "license": "MIT",

"engines": {
"node": ">=0.10.0"
"node": ">=4"
},

@@ -27,2 +27,3 @@ "scripts": {

"promisify",
"all",
"denodify",

@@ -42,10 +43,12 @@ "denodeify",

"async",
"es2015"
"await",
"es2015",
"bluebird"
],
"devDependencies": {
"ava": "*",
"pinkie-promise": "^1.0.0",
"v8-natives": "0.0.2",
"pinkie-promise": "^2.0.0",
"v8-natives": "^1.0.0",
"xo": "*"
}
}

@@ -19,4 +19,3 @@ # pify [![Build Status](https://travis-ci.org/sindresorhus/pify.svg?branch=master)](https://travis-ci.org/sindresorhus/pify)

// promisify a single function
// Promisify a single function
pify(fs.readFile)('package.json', 'utf8').then(data => {

@@ -27,4 +26,3 @@ console.log(JSON.parse(data).name);

// or promisify all methods in a module
// Promisify all methods in a module
pify(fs).readFile('package.json', 'utf8').then(data => {

@@ -39,20 +37,12 @@ console.log(JSON.parse(data).name);

### pify(input, [promiseModule], [options])
### pify(input, [options])
Returns a promise wrapped version of the supplied function or module.
Returns a `Promise` wrapped version of the supplied function or module.
#### input
Type: `function`, `object`
Type: `Function` `Object`
Callback-style function or module whose methods you want to promisify.
#### promiseModule
Type: `function`
Custom promise module to use instead of the native one.
Check out [`pinkie-promise`](https://github.com/floatdrop/pinkie-promise) if you need a tiny promise polyfill.
#### options

@@ -62,6 +52,6 @@

Type: `boolean`
Type: `boolean`<br>
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.
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. This also applies to rejections, where it returns an array of all the callback arguments, including the error.

@@ -79,3 +69,3 @@ ```js

Type: `array` of (`string`|`regex`)
Type: `string[]` `RegExp[]`

@@ -86,4 +76,4 @@ Methods in a module to promisify. Remaining methods will be left untouched.

Type: `array` of (`string`|`regex`)
Default: `[/.+Sync$/]`
Type: `string[]` `RegExp[]`<br>
Default: `[/.+(Sync|Stream)$/]`

@@ -94,6 +84,6 @@ Methods in a module **not** to promisify. Methods with names ending with `'Sync'` are excluded by default.

Type: `boolean`
Type: `boolean`<br>
Default: `false`
By default, if given module is a function itself, this function will be promisified. Turn this option on if you want to promisify only methods of the module.
If given module is a function itself, it will be promisified. Turn this option on if you want to promisify only methods of the module.

@@ -109,7 +99,7 @@ ```js

setImmediate(() => {
callback(data, null);
callback(null, data);
});
};
// promisify methods but not fn()
// Promisify methods but not `fn()`
const promiseFn = pify(fn, {excludeMain: true});

@@ -124,5 +114,27 @@

##### errorFirst
Type: `boolean`<br>
Default: `true`
Whether the callback has an error as the first argument. You'll want to set this to `false` if you're dealing with an API that doesn't have an error as the first argument, like `fs.exists()`, some browser APIs, Chrome Extension APIs, etc.
##### promiseModule
Type: `Function`
Custom promise module to use instead of the native one.
Check out [`pinkie-promise`](https://github.com/floatdrop/pinkie-promise) if you need a tiny promise polyfill.
## Related
- [p-event](https://github.com/sindresorhus/p-event) - Promisify an event by waiting for it to be emitted
- [p-map](https://github.com/sindresorhus/p-map) - Map over promises concurrently
- [More…](https://github.com/sindresorhus/promise-fun)
## License
MIT © [Sindre Sorhus](http://sindresorhus.com)
MIT © [Sindre Sorhus](https://sindresorhus.com)

Sorry, the diff of this file is not supported yet

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