1 # pify [![Build Status](https://travis-ci.org/sindresorhus/pify.svg?branch=master)](https://travis-ci.org/sindresorhus/pify)
3 > Promisify a callback-style function
9 $ npm install --save pify
16 const fs = require('fs');
17 const pify = require('pify');
19 // promisify a single function
21 pify(fs.readFile)('package.json', 'utf8').then(data => {
22 console.log(JSON.parse(data).name);
26 // or promisify all methods in a module
28 pify(fs).readFile('package.json', 'utf8').then(data => {
29 console.log(JSON.parse(data).name);
37 ### pify(input, [promiseModule], [options])
39 Returns a promise wrapped version of the supplied function or module.
43 Type: `function`, `object`
45 Callback-style function or module whose methods you want to promisify.
51 Custom promise module to use instead of the native one.
53 Check out [`pinkie-promise`](https://github.com/floatdrop/pinkie-promise) if you need a tiny promise polyfill.
62 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.
65 const request = require('request');
66 const pify = require('pify');
68 pify(request, {multiArgs: true})('https://sindresorhus.com').then(result => {
69 const [httpResponse, body] = result;
75 Type: `array` of (`string`|`regex`)
77 Methods in a module to promisify. Remaining methods will be left untouched.
81 Type: `array` of (`string`|`regex`)
82 Default: `[/.+Sync$/]`
84 Methods in a module **not** to promisify. Methods with names ending with `'Sync'` are excluded by default.
91 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.
94 const pify = require('pify');
100 fn.method = (data, callback) => {
102 callback(data, null);
106 // promisify methods but not fn()
107 const promiseFn = pify(fn, {excludeMain: true});
110 promiseFn.method('hi').then(data => {
119 MIT © [Sindre Sorhus](http://sindresorhus.com)