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

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

primeNG components

  • Property mode set to 100644
File size: 3.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
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
30const fs = require('fs');
31const pify = require('pify');
32
33// Promisify a single function
34pify(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
40pify(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
51Returns a `Promise` wrapped version of the supplied function or module.
52
53#### input
54
55Type: `Function` `Object`
56
57Callback-style function or module whose methods you want to promisify.
58
59#### options
60
61##### multiArgs
62
63Type: `boolean`<br>
64Default: `false`
65
66By 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
69const request = require('request');
70const pify = require('pify');
71
72pify(request, {multiArgs: true})('https://sindresorhus.com').then(result => {
73 const [httpResponse, body] = result;
74});
75```
76
77##### include
78
79Type: `string[]` `RegExp[]`
80
81Methods in a module to promisify. Remaining methods will be left untouched.
82
83##### exclude
84
85Type: `string[]` `RegExp[]`<br>
86Default: `[/.+(Sync|Stream)$/]`
87
88Methods in a module **not** to promisify. Methods with names ending with `'Sync'` are excluded by default.
89
90##### excludeMain
91
92Type: `boolean`<br>
93Default: `false`
94
95If 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
98const pify = require('pify');
99
100function fn() {
101 return true;
102}
103
104fn.method = (data, callback) => {
105 setImmediate(() => {
106 callback(null, data);
107 });
108};
109
110// Promisify methods but not `fn()`
111const promiseFn = pify(fn, {excludeMain: true});
112
113if (promiseFn()) {
114 promiseFn.method('hi').then(data => {
115 console.log(data);
116 });
117}
118```
119
120##### errorFirst
121
122Type: `boolean`<br>
123Default: `true`
124
125Whether 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
129Type: `Function`
130
131Custom promise module to use instead of the native one.
132
133Check 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
145MIT © [Sindre Sorhus](https://sindresorhus.com)
Note: See TracBrowser for help on using the repository browser.