wishthis/node_modules/pify/readme.md

120 lines
2.5 KiB
Markdown
Raw Normal View History

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