\begin{quote}
Promisify a callback-\/style function \end{quote}
\hypertarget{md_sfcode_node_modules_pify_readme_autotoc_md20389}{}\doxysection{$<$/blockquote$>$}\label{md_sfcode_node_modules_pify_readme_autotoc_md20389}
 {\bfseries{ \href{https://tidelift.com/subscription/pkg/npm-pify?utm_source=npm-pify&utm_medium=referral&utm_campaign=readme}{\texttt{ Get professional support for \textquotesingle{}pify\textquotesingle{} with a Tidelift subscription}} }} ~\newline
 \textsubscript{ Tidelift helps make open source sustainable for maintainers while giving companies~\newline
assurances about security, maintenance, and licensing for their dependencies. } 

\DoxyHorRuler{0}
\hypertarget{md_sfcode_node_modules_pify_readme_autotoc_md20391}{}\doxysection{Install}\label{md_sfcode_node_modules_pify_readme_autotoc_md20391}

\begin{DoxyCode}{0}
\DoxyCodeLine{\$ npm install pify }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_pify_readme_autotoc_md20392}{}\doxysection{Usage}\label{md_sfcode_node_modules_pify_readme_autotoc_md20392}

\begin{DoxyCode}{0}
\DoxyCodeLine{const fs = require('fs');}
\DoxyCodeLine{const pify = require('pify');}
\DoxyCodeLine{}
\DoxyCodeLine{// Promisify a single function}
\DoxyCodeLine{pify(fs.readFile)('package.json', 'utf8').then(data => \{}
\DoxyCodeLine{    console.log(JSON.parse(data).name);}
\DoxyCodeLine{    //=> 'pify'}
\DoxyCodeLine{\});}
\DoxyCodeLine{}
\DoxyCodeLine{// Promisify all methods in a module}
\DoxyCodeLine{pify(fs).readFile('package.json', 'utf8').then(data => \{}
\DoxyCodeLine{    console.log(JSON.parse(data).name);}
\DoxyCodeLine{    //=> 'pify'}
\DoxyCodeLine{\}); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_pify_readme_autotoc_md20393}{}\doxysection{API}\label{md_sfcode_node_modules_pify_readme_autotoc_md20393}
\hypertarget{md_sfcode_node_modules_pify_readme_autotoc_md20394}{}\doxysubsection{pify(input, \mbox{[}options\mbox{]})}\label{md_sfcode_node_modules_pify_readme_autotoc_md20394}
Returns a {\ttfamily Promise} wrapped version of the supplied function or module.\hypertarget{md_sfcode_node_modules_pify_readme_autotoc_md20395}{}\doxysubsubsection{input}\label{md_sfcode_node_modules_pify_readme_autotoc_md20395}
Type\+: {\ttfamily Function} {\ttfamily Object}

Callback-\/style function or module whose methods you want to promisify.\hypertarget{md_sfcode_node_modules_pify_readme_autotoc_md20396}{}\doxysubsubsection{options}\label{md_sfcode_node_modules_pify_readme_autotoc_md20396}
\hypertarget{md_sfcode_node_modules_pify_readme_autotoc_md20397}{}\doxyparagraph{multi\+Args}\label{md_sfcode_node_modules_pify_readme_autotoc_md20397}
Type\+: {\ttfamily boolean}~\newline
 Default\+: {\ttfamily false}

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 {\ttfamily 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.


\begin{DoxyCode}{0}
\DoxyCodeLine{const request = require('request');}
\DoxyCodeLine{const pify = require('pify');}
\DoxyCodeLine{}
\DoxyCodeLine{pify(request, \{multiArgs: true\})('https://sindresorhus.com').then(result => \{}
\DoxyCodeLine{    const [httpResponse, body] = result;}
\DoxyCodeLine{\}); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_pify_readme_autotoc_md20398}{}\doxyparagraph{include}\label{md_sfcode_node_modules_pify_readme_autotoc_md20398}
Type\+: {\ttfamily string\mbox{[}\mbox{]}} {\ttfamily Reg\+Exp\mbox{[}\mbox{]}}

Methods in a module to promisify. Remaining methods will be left untouched.\hypertarget{md_sfcode_node_modules_pify_readme_autotoc_md20399}{}\doxyparagraph{exclude}\label{md_sfcode_node_modules_pify_readme_autotoc_md20399}
Type\+: {\ttfamily string\mbox{[}\mbox{]}} {\ttfamily Reg\+Exp\mbox{[}\mbox{]}}~\newline
 Default\+: {\ttfamily \mbox{[}/.+(Sync$\vert$\+Stream)\$/\mbox{]}}

Methods in a module {\bfseries{not}} to promisify. Methods with names ending with `\textquotesingle{}Sync'\`{} are excluded by default.\hypertarget{md_sfcode_node_modules_pify_readme_autotoc_md20400}{}\doxyparagraph{exclude\+Main}\label{md_sfcode_node_modules_pify_readme_autotoc_md20400}
Type\+: {\ttfamily boolean}~\newline
 Default\+: {\ttfamily false}

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.


\begin{DoxyCode}{0}
\DoxyCodeLine{const pify = require('pify');}
\DoxyCodeLine{}
\DoxyCodeLine{function fn() \{}
\DoxyCodeLine{    return true;}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{fn.method = (data, callback) => \{}
\DoxyCodeLine{    setImmediate(() => \{}
\DoxyCodeLine{        callback(null, data);}
\DoxyCodeLine{    \});}
\DoxyCodeLine{\};}
\DoxyCodeLine{}
\DoxyCodeLine{// Promisify methods but not `fn()`}
\DoxyCodeLine{const promiseFn = pify(fn, \{excludeMain: true\});}
\DoxyCodeLine{}
\DoxyCodeLine{if (promiseFn()) \{}
\DoxyCodeLine{    promiseFn.method('hi').then(data => \{}
\DoxyCodeLine{        console.log(data);}
\DoxyCodeLine{    \});}
\DoxyCodeLine{\} }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_pify_readme_autotoc_md20401}{}\doxyparagraph{error\+First}\label{md_sfcode_node_modules_pify_readme_autotoc_md20401}
Type\+: {\ttfamily boolean}~\newline
 Default\+: {\ttfamily true}

Whether the callback has an error as the first argument. You\textquotesingle{}ll want to set this to {\ttfamily false} if you\textquotesingle{}re dealing with an API that doesn\textquotesingle{}t have an error as the first argument, like {\ttfamily fs.\+exists()}, some browser APIs, Chrome Extension APIs, etc.\hypertarget{md_sfcode_node_modules_pify_readme_autotoc_md20402}{}\doxyparagraph{promise\+Module}\label{md_sfcode_node_modules_pify_readme_autotoc_md20402}
Type\+: {\ttfamily Function}

Custom promise module to use instead of the native one.

Check out \href{https://github.com/floatdrop/pinkie-promise}{\texttt{ {\ttfamily pinkie-\/promise}}} if you need a tiny promise polyfill.\hypertarget{md_sfcode_node_modules_pify_readme_autotoc_md20403}{}\doxysection{Related}\label{md_sfcode_node_modules_pify_readme_autotoc_md20403}

\begin{DoxyItemize}
\item \href{https://github.com/sindresorhus/p-event}{\texttt{ p-\/event}} -\/ Promisify an event by waiting for it to be emitted
\item \href{https://github.com/sindresorhus/p-map}{\texttt{ p-\/map}} -\/ Map over promises concurrently
\item \href{https://github.com/sindresorhus/promise-fun}{\texttt{ More…}}
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_pify_readme_autotoc_md20404}{}\doxysection{License}\label{md_sfcode_node_modules_pify_readme_autotoc_md20404}
MIT © \href{https://sindresorhus.com}{\texttt{ Sindre Sorhus}} 