2023-07-19 21:31:30 +02:00
# pify [](https://travis-ci.org/sindresorhus/pify)
> Promisify a callback-style function
## Install
```
2023-07-20 20:28:44 +02:00
$ npm install --save pify
2023-07-19 21:31:30 +02:00
```
## Usage
```js
const fs = require('fs');
const pify = require('pify');
2023-07-20 20:28:44 +02:00
// promisify a single function
2023-07-19 21:31:30 +02:00
pify(fs.readFile)('package.json', 'utf8').then(data => {
console.log(JSON.parse(data).name);
//=> 'pify'
});
2023-07-20 20:28:44 +02:00
// or promisify all methods in a module
2023-07-19 21:31:30 +02:00
pify(fs).readFile('package.json', 'utf8').then(data => {
console.log(JSON.parse(data).name);
//=> 'pify'
});
```
## API
2023-07-20 20:28:44 +02:00
### pify(input, [promiseModule], [options])
2023-07-19 21:31:30 +02:00
2023-07-20 20:28:44 +02:00
Returns a promise wrapped version of the supplied function or module.
2023-07-19 21:31:30 +02:00
#### input
2023-07-20 20:28:44 +02:00
Type: `function` , `object`
2023-07-19 21:31:30 +02:00
Callback-style function or module whose methods you want to promisify.
2023-07-20 20:28:44 +02:00
#### 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.
2023-07-19 21:31:30 +02:00
#### options
##### multiArgs
2023-07-20 20:28:44 +02:00
Type: `boolean`
2023-07-19 21:31:30 +02:00
Default: `false`
2023-07-20 20:28:44 +02:00
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.
2023-07-19 21:31:30 +02:00
```js
const request = require('request');
const pify = require('pify');
pify(request, {multiArgs: true})('https://sindresorhus.com').then(result => {
const [httpResponse, body] = result;
});
```
##### include
2023-07-20 20:28:44 +02:00
Type: `array` of (`string` |`regex` )
2023-07-19 21:31:30 +02:00
Methods in a module to promisify. Remaining methods will be left untouched.
##### exclude
2023-07-20 20:28:44 +02:00
Type: `array` of (`string` |`regex` )
Default: `[/.+Sync$/]`
2023-07-19 21:31:30 +02:00
Methods in a module **not** to promisify. Methods with names ending with `'Sync'` are excluded by default.
##### excludeMain
2023-07-20 20:28:44 +02:00
Type: `boolean`
2023-07-19 21:31:30 +02:00
Default: `false`
2023-07-20 20:28:44 +02:00
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.
2023-07-19 21:31:30 +02:00
```js
const pify = require('pify');
function fn() {
return true;
}
fn.method = (data, callback) => {
setImmediate(() => {
2023-07-20 20:28:44 +02:00
callback(data, null);
2023-07-19 21:31:30 +02:00
});
};
2023-07-20 20:28:44 +02:00
// promisify methods but not fn()
2023-07-19 21:31:30 +02:00
const promiseFn = pify(fn, {excludeMain: true});
if (promiseFn()) {
promiseFn.method('hi').then(data => {
console.log(data);
});
}
```
## License
2023-07-20 20:28:44 +02:00
MIT © [Sindre Sorhus ](http://sindresorhus.com )