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

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

primeNG components

  • Property mode set to 100644
File size: 3.5 KB
RevLine 
[6a3a178]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
[e29cc2e]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
[6a3a178]19
20## Install
21
22```
[e29cc2e]23$ npm install pify
[6a3a178]24```
25
26
27## Usage
28
29```js
30const fs = require('fs');
31const pify = require('pify');
32
[e29cc2e]33// Promisify a single function
[6a3a178]34pify(fs.readFile)('package.json', 'utf8').then(data => {
35 console.log(JSON.parse(data).name);
36 //=> 'pify'
37});
38
[e29cc2e]39// Promisify all methods in a module
[6a3a178]40pify(fs).readFile('package.json', 'utf8').then(data => {
41 console.log(JSON.parse(data).name);
42 //=> 'pify'
43});
44```
45
46
47## API
48
[e29cc2e]49### pify(input, [options])
[6a3a178]50
[e29cc2e]51Returns a `Promise` wrapped version of the supplied function or module.
[6a3a178]52
53#### input
54
[e29cc2e]55Type: `Function` `Object`
[6a3a178]56
57Callback-style function or module whose methods you want to promisify.
58
59#### options
60
61##### multiArgs
62
[e29cc2e]63Type: `boolean`<br>
[6a3a178]64Default: `false`
65
[e29cc2e]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.
[6a3a178]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
[e29cc2e]79Type: `string[]` `RegExp[]`
[6a3a178]80
81Methods in a module to promisify. Remaining methods will be left untouched.
82
83##### exclude
84
[e29cc2e]85Type: `string[]` `RegExp[]`<br>
86Default: `[/.+(Sync|Stream)$/]`
[6a3a178]87
88Methods in a module **not** to promisify. Methods with names ending with `'Sync'` are excluded by default.
89
90##### excludeMain
91
[e29cc2e]92Type: `boolean`<br>
[6a3a178]93Default: `false`
94
[e29cc2e]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.
[6a3a178]96
97```js
98const pify = require('pify');
99
100function fn() {
101 return true;
102}
103
104fn.method = (data, callback) => {
105 setImmediate(() => {
[e29cc2e]106 callback(null, data);
[6a3a178]107 });
108};
109
[e29cc2e]110// Promisify methods but not `fn()`
[6a3a178]111const promiseFn = pify(fn, {excludeMain: true});
112
113if (promiseFn()) {
114 promiseFn.method('hi').then(data => {
115 console.log(data);
116 });
117}
118```
119
[e29cc2e]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
[6a3a178]142
143## License
144
[e29cc2e]145MIT © [Sindre Sorhus](https://sindresorhus.com)
Note: See TracBrowser for help on using the repository browser.