1
|
# pify [![Build Status](https://travis-ci.org/sindresorhus/pify.svg?branch=master)](https://travis-ci.org/sindresorhus/pify)
|
2
|
|
3
|
> Promisify a callback-style function
|
4
|
|
5
|
---
|
6
|
|
7
|
<div align="center">
|
8
|
<b>
|
9
|
<a href="https://tidelift.com/subscription/pkg/npm-pify?utm_source=npm-pify&utm_medium=referral&utm_campaign=readme">Get professional support for 'pify' with a Tidelift subscription</a>
|
10
|
</b>
|
11
|
<br>
|
12
|
<sub>
|
13
|
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
|
14
|
</sub>
|
15
|
</div>
|
16
|
|
17
|
---
|
18
|
|
19
|
|
20
|
## Install
|
21
|
|
22
|
```
|
23
|
$ npm install pify
|
24
|
```
|
25
|
|
26
|
|
27
|
## Usage
|
28
|
|
29
|
```js
|
30
|
const fs = require('fs');
|
31
|
const pify = require('pify');
|
32
|
|
33
|
// Promisify a single function
|
34
|
pify(fs.readFile)('package.json', 'utf8').then(data => {
|
35
|
console.log(JSON.parse(data).name);
|
36
|
//=> 'pify'
|
37
|
});
|
38
|
|
39
|
// Promisify all methods in a module
|
40
|
pify(fs).readFile('package.json', 'utf8').then(data => {
|
41
|
console.log(JSON.parse(data).name);
|
42
|
//=> 'pify'
|
43
|
});
|
44
|
```
|
45
|
|
46
|
|
47
|
## API
|
48
|
|
49
|
### pify(input, [options])
|
50
|
|
51
|
Returns a `Promise` wrapped version of the supplied function or module.
|
52
|
|
53
|
#### input
|
54
|
|
55
|
Type: `Function` `Object`
|
56
|
|
57
|
Callback-style function or module whose methods you want to promisify.
|
58
|
|
59
|
#### options
|
60
|
|
61
|
##### multiArgs
|
62
|
|
63
|
Type: `boolean`<br>
|
64
|
Default: `false`
|
65
|
|
66
|
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.
|
67
|
|
68
|
```js
|
69
|
const request = require('request');
|
70
|
const pify = require('pify');
|
71
|
|
72
|
pify(request, {multiArgs: true})('https://sindresorhus.com').then(result => {
|
73
|
const [httpResponse, body] = result;
|
74
|
});
|
75
|
```
|
76
|
|
77
|
##### include
|
78
|
|
79
|
Type: `string[]` `RegExp[]`
|
80
|
|
81
|
Methods in a module to promisify. Remaining methods will be left untouched.
|
82
|
|
83
|
##### exclude
|
84
|
|
85
|
Type: `string[]` `RegExp[]`<br>
|
86
|
Default: `[/.+(Sync|Stream)$/]`
|
87
|
|
88
|
Methods in a module **not** to promisify. Methods with names ending with `'Sync'` are excluded by default.
|
89
|
|
90
|
##### excludeMain
|
91
|
|
92
|
Type: `boolean`<br>
|
93
|
Default: `false`
|
94
|
|
95
|
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.
|
96
|
|
97
|
```js
|
98
|
const pify = require('pify');
|
99
|
|
100
|
function fn() {
|
101
|
return true;
|
102
|
}
|
103
|
|
104
|
fn.method = (data, callback) => {
|
105
|
setImmediate(() => {
|
106
|
callback(null, data);
|
107
|
});
|
108
|
};
|
109
|
|
110
|
// Promisify methods but not `fn()`
|
111
|
const promiseFn = pify(fn, {excludeMain: true});
|
112
|
|
113
|
if (promiseFn()) {
|
114
|
promiseFn.method('hi').then(data => {
|
115
|
console.log(data);
|
116
|
});
|
117
|
}
|
118
|
```
|
119
|
|
120
|
##### errorFirst
|
121
|
|
122
|
Type: `boolean`<br>
|
123
|
Default: `true`
|
124
|
|
125
|
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.
|
126
|
|
127
|
##### promiseModule
|
128
|
|
129
|
Type: `Function`
|
130
|
|
131
|
Custom promise module to use instead of the native one.
|
132
|
|
133
|
Check out [`pinkie-promise`](https://github.com/floatdrop/pinkie-promise) if you need a tiny promise polyfill.
|
134
|
|
135
|
|
136
|
## Related
|
137
|
|
138
|
- [p-event](https://github.com/sindresorhus/p-event) - Promisify an event by waiting for it to be emitted
|
139
|
- [p-map](https://github.com/sindresorhus/p-map) - Map over promises concurrently
|
140
|
- [More…](https://github.com/sindresorhus/promise-fun)
|
141
|
|
142
|
|
143
|
## License
|
144
|
|
145
|
MIT © [Sindre Sorhus](https://sindresorhus.com)
|