You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
3.5 KiB

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