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 20pify(fs.readFile)('package.json', 'utf8').then(data => { 21 console.log(JSON.parse(data).name); 22 //=> 'pify' 23}); 24 25// Promisify all methods in a module 26pify(fs).readFile('package.json', 'utf8').then(data => { 27 console.log(JSON.parse(data).name); 28 //=> 'pify' 29}); 30``` 31 32 33## API 34 35### pify(input, [options]) 36 37Returns a `Promise` wrapped version of the supplied function or module. 38 39#### input 40 41Type: `Function` `Object` 42 43Callback-style function or module whose methods you want to promisify. 44 45#### options 46 47##### multiArgs 48 49Type: `boolean`<br> 50Default: `false` 51 52By 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. 53 54```js 55const request = require('request'); 56const pify = require('pify'); 57 58pify(request, {multiArgs: true})('https://sindresorhus.com').then(result => { 59 const [httpResponse, body] = result; 60}); 61``` 62 63##### include 64 65Type: `string[]` `RegExp[]` 66 67Methods in a module to promisify. Remaining methods will be left untouched. 68 69##### exclude 70 71Type: `string[]` `RegExp[]`<br> 72Default: `[/.+(Sync|Stream)$/]` 73 74Methods in a module **not** to promisify. Methods with names ending with `'Sync'` are excluded by default. 75 76##### excludeMain 77 78Type: `boolean`<br> 79Default: `false` 80 81If given module is a function itself, it will be promisified. Turn this option on if you want to promisify only methods of the module. 82 83```js 84const pify = require('pify'); 85 86function fn() { 87 return true; 88} 89 90fn.method = (data, callback) => { 91 setImmediate(() => { 92 callback(null, data); 93 }); 94}; 95 96// Promisify methods but not `fn()` 97const promiseFn = pify(fn, {excludeMain: true}); 98 99if (promiseFn()) { 100 promiseFn.method('hi').then(data => { 101 console.log(data); 102 }); 103} 104``` 105 106##### errorFirst 107 108Type: `boolean`<br> 109Default: `true` 110 111Whether 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. 112 113##### promiseModule 114 115Type: `Function` 116 117Custom promise module to use instead of the native one. 118 119Check out [`pinkie-promise`](https://github.com/floatdrop/pinkie-promise) if you need a tiny promise polyfill. 120 121 122## Related 123 124- [p-event](https://github.com/sindresorhus/p-event) - Promisify an event by waiting for it to be emitted 125- [p-map](https://github.com/sindresorhus/p-map) - Map over promises concurrently 126- [More…](https://github.com/sindresorhus/promise-fun) 127 128 129## License 130 131MIT © [Sindre Sorhus](https://sindresorhus.com) 132