source: trip-planner-front/node_modules/pify/readme.md@ 6c1585f

Last change on this file since 6c1585f was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.5 KB
Line 
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
16const fs = require('fs');
17const pify = require('pify');
18
19// promisify a single function
20
21pify(fs.readFile)('package.json', 'utf8').then(data => {
22 console.log(JSON.parse(data).name);
23 //=> 'pify'
24});
25
26// or promisify all methods in a module
27
28pify(fs).readFile('package.json', 'utf8').then(data => {
29 console.log(JSON.parse(data).name);
30 //=> 'pify'
31});
32```
33
34
35## API
36
37### pify(input, [promiseModule], [options])
38
39Returns a promise wrapped version of the supplied function or module.
40
41#### input
42
43Type: `function`, `object`
44
45Callback-style function or module whose methods you want to promisify.
46
47#### promiseModule
48
49Type: `function`
50
51Custom promise module to use instead of the native one.
52
53Check out [`pinkie-promise`](https://github.com/floatdrop/pinkie-promise) if you need a tiny promise polyfill.
54
55#### options
56
57##### multiArgs
58
59Type: `boolean`
60Default: `false`
61
62By 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.
63
64```js
65const request = require('request');
66const pify = require('pify');
67
68pify(request, {multiArgs: true})('https://sindresorhus.com').then(result => {
69 const [httpResponse, body] = result;
70});
71```
72
73##### include
74
75Type: `array` of (`string`|`regex`)
76
77Methods in a module to promisify. Remaining methods will be left untouched.
78
79##### exclude
80
81Type: `array` of (`string`|`regex`)
82Default: `[/.+Sync$/]`
83
84Methods in a module **not** to promisify. Methods with names ending with `'Sync'` are excluded by default.
85
86##### excludeMain
87
88Type: `boolean`
89Default: `false`
90
91By 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.
92
93```js
94const pify = require('pify');
95
96function fn() {
97 return true;
98}
99
100fn.method = (data, callback) => {
101 setImmediate(() => {
102 callback(data, null);
103 });
104};
105
106// promisify methods but not fn()
107const promiseFn = pify(fn, {excludeMain: true});
108
109if (promiseFn()) {
110 promiseFn.method('hi').then(data => {
111 console.log(data);
112 });
113}
114```
115
116
117## License
118
119MIT © [Sindre Sorhus](http://sindresorhus.com)
Note: See TracBrowser for help on using the repository browser.