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.

119 lines
2.5 KiB

2 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. ## Install
  4. ```
  5. $ npm install --save pify
  6. ```
  7. ## Usage
  8. ```js
  9. const fs = require('fs');
  10. const pify = require('pify');
  11. // promisify a single function
  12. pify(fs.readFile)('package.json', 'utf8').then(data => {
  13. console.log(JSON.parse(data).name);
  14. //=> 'pify'
  15. });
  16. // or promisify all methods in a module
  17. pify(fs).readFile('package.json', 'utf8').then(data => {
  18. console.log(JSON.parse(data).name);
  19. //=> 'pify'
  20. });
  21. ```
  22. ## API
  23. ### pify(input, [promiseModule], [options])
  24. Returns a promise wrapped version of the supplied function or module.
  25. #### input
  26. Type: `function`, `object`
  27. Callback-style function or module whose methods you want to promisify.
  28. #### promiseModule
  29. Type: `function`
  30. Custom promise module to use instead of the native one.
  31. Check out [`pinkie-promise`](https://github.com/floatdrop/pinkie-promise) if you need a tiny promise polyfill.
  32. #### options
  33. ##### multiArgs
  34. Type: `boolean`
  35. Default: `false`
  36. 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.
  37. ```js
  38. const request = require('request');
  39. const pify = require('pify');
  40. pify(request, {multiArgs: true})('https://sindresorhus.com').then(result => {
  41. const [httpResponse, body] = result;
  42. });
  43. ```
  44. ##### include
  45. Type: `array` of (`string`|`regex`)
  46. Methods in a module to promisify. Remaining methods will be left untouched.
  47. ##### exclude
  48. Type: `array` of (`string`|`regex`)
  49. Default: `[/.+Sync$/]`
  50. Methods in a module **not** to promisify. Methods with names ending with `'Sync'` are excluded by default.
  51. ##### excludeMain
  52. Type: `boolean`
  53. Default: `false`
  54. By 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.
  55. ```js
  56. const pify = require('pify');
  57. function fn() {
  58. return true;
  59. }
  60. fn.method = (data, callback) => {
  61. setImmediate(() => {
  62. callback(data, null);
  63. });
  64. };
  65. // promisify methods but not fn()
  66. const promiseFn = pify(fn, {excludeMain: true});
  67. if (promiseFn()) {
  68. promiseFn.method('hi').then(data => {
  69. console.log(data);
  70. });
  71. }
  72. ```
  73. ## License
  74. MIT © [Sindre Sorhus](http://sindresorhus.com)