.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / pify / readme.md
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 ## Install
7
8 ```
9 $ npm install --save pify
10 ```
11
12
13 ## Usage
14
15 ```js
16 const fs = require('fs');
17 const pify = require('pify');
18
19 // Promisify a single function
20 pify(fs.readFile)('package.json', 'utf8').then(data => {
21         console.log(JSON.parse(data).name);
22         //=> 'pify'
23 });
24
25 // Promisify all methods in a module
26 pify(fs).readFile('package.json', 'utf8').then(data => {
27         console.log(JSON.parse(data).name);
28         //=> 'pify'
29 });
30 ```
31
32
33 ## API
34
35 ### pify(input, [options])
36
37 Returns a `Promise` wrapped version of the supplied function or module.
38
39 #### input
40
41 Type: `Function` `Object`
42
43 Callback-style function or module whose methods you want to promisify.
44
45 #### options
46
47 ##### multiArgs
48
49 Type: `boolean`<br>
50 Default: `false`
51
52 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.
53
54 ```js
55 const request = require('request');
56 const pify = require('pify');
57
58 pify(request, {multiArgs: true})('https://sindresorhus.com').then(result => {
59         const [httpResponse, body] = result;
60 });
61 ```
62
63 ##### include
64
65 Type: `string[]` `RegExp[]`
66
67 Methods in a module to promisify. Remaining methods will be left untouched.
68
69 ##### exclude
70
71 Type: `string[]` `RegExp[]`<br>
72 Default: `[/.+(Sync|Stream)$/]`
73
74 Methods in a module **not** to promisify. Methods with names ending with `'Sync'` are excluded by default.
75
76 ##### excludeMain
77
78 Type: `boolean`<br>
79 Default: `false`
80
81 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.
82
83 ```js
84 const pify = require('pify');
85
86 function fn() {
87         return true;
88 }
89
90 fn.method = (data, callback) => {
91         setImmediate(() => {
92                 callback(null, data);
93         });
94 };
95
96 // Promisify methods but not `fn()`
97 const promiseFn = pify(fn, {excludeMain: true});
98
99 if (promiseFn()) {
100         promiseFn.method('hi').then(data => {
101                 console.log(data);
102         });
103 }
104 ```
105
106 ##### errorFirst
107
108 Type: `boolean`<br>
109 Default: `true`
110
111 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.
112
113 ##### promiseModule
114
115 Type: `Function`
116
117 Custom promise module to use instead of the native one.
118
119 Check out [`pinkie-promise`](https://github.com/floatdrop/pinkie-promise) if you need a tiny promise polyfill.
120
121
122 ## Related
123
124 - [p-event](https://github.com/sindresorhus/p-event) - Promisify an event by waiting for it to be emitted
125 - [p-map](https://github.com/sindresorhus/p-map) - Map over promises concurrently
126 - [More…](https://github.com/sindresorhus/promise-fun)
127
128
129 ## License
130
131 MIT © [Sindre Sorhus](https://sindresorhus.com)