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
20 pify(fs.readFile)('package.json', 'utf8').then(data => {
21 console.log(JSON.parse(data).name);
25 // Promisify all methods in a module
26 pify(fs).readFile('package.json', 'utf8').then(data => {
27 console.log(JSON.parse(data).name);
35 ### pify(input, [options])
37 Returns a `Promise` wrapped version of the supplied function or module.
41 Type: `Function` `Object`
43 Callback-style function or module whose methods you want to promisify.
52 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.
55 const request = require('request');
56 const pify = require('pify');
58 pify(request, {multiArgs: true})('https://sindresorhus.com').then(result => {
59 const [httpResponse, body] = result;
65 Type: `string[]` `RegExp[]`
67 Methods in a module to promisify. Remaining methods will be left untouched.
71 Type: `string[]` `RegExp[]`<br>
72 Default: `[/.+(Sync|Stream)$/]`
74 Methods in a module **not** to promisify. Methods with names ending with `'Sync'` are excluded by default.
81 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.
84 const pify = require('pify');
90 fn.method = (data, callback) => {
96 // Promisify methods but not `fn()`
97 const promiseFn = pify(fn, {excludeMain: true});
100 promiseFn.method('hi').then(data => {
111 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.
117 Custom promise module to use instead of the native one.
119 Check out [`pinkie-promise`](https://github.com/floatdrop/pinkie-promise) if you need a tiny promise polyfill.
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)
131 MIT © [Sindre Sorhus](https://sindresorhus.com)