\begin{quote}
Promisify a callback-\/style function \end{quote}
\hypertarget{md_sfcode_node_modules_read_cache_node_modules_pify_readme_autotoc_md23918}{}\doxysection{Install}\label{md_sfcode_node_modules_read_cache_node_modules_pify_readme_autotoc_md23918}

\begin{DoxyCode}{0}
\DoxyCodeLine{\$ npm install -\/-\/save pify }

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

\begin{DoxyCode}{0}
\DoxyCodeLine{const fs = require('fs');}
\DoxyCodeLine{const pify = require('pify');}
\DoxyCodeLine{}
\DoxyCodeLine{// promisify a single function}
\DoxyCodeLine{}
\DoxyCodeLine{pify(fs.readFile)('package.json', 'utf8').then(data => \{}
\DoxyCodeLine{    console.log(JSON.parse(data).name);}
\DoxyCodeLine{    //=> 'pify'}
\DoxyCodeLine{\});}
\DoxyCodeLine{}
\DoxyCodeLine{// or promisify all methods in a module}
\DoxyCodeLine{}
\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_read_cache_node_modules_pify_readme_autotoc_md23920}{}\doxysection{API}\label{md_sfcode_node_modules_read_cache_node_modules_pify_readme_autotoc_md23920}
\hypertarget{md_sfcode_node_modules_read_cache_node_modules_pify_readme_autotoc_md23921}{}\doxysubsection{pify(input, \mbox{[}promise\+Module\mbox{]}, \mbox{[}options\mbox{]})}\label{md_sfcode_node_modules_read_cache_node_modules_pify_readme_autotoc_md23921}
Returns a promise wrapped version of the supplied function or module.\hypertarget{md_sfcode_node_modules_read_cache_node_modules_pify_readme_autotoc_md23922}{}\doxysubsubsection{input}\label{md_sfcode_node_modules_read_cache_node_modules_pify_readme_autotoc_md23922}
Type\+: {\ttfamily function}, {\ttfamily object}

Callback-\/style function or module whose methods you want to promisify.\hypertarget{md_sfcode_node_modules_read_cache_node_modules_pify_readme_autotoc_md23923}{}\doxysubsubsection{promise\+Module}\label{md_sfcode_node_modules_read_cache_node_modules_pify_readme_autotoc_md23923}
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_read_cache_node_modules_pify_readme_autotoc_md23924}{}\doxysubsubsection{options}\label{md_sfcode_node_modules_read_cache_node_modules_pify_readme_autotoc_md23924}
\hypertarget{md_sfcode_node_modules_read_cache_node_modules_pify_readme_autotoc_md23925}{}\doxyparagraph{multi\+Args}\label{md_sfcode_node_modules_read_cache_node_modules_pify_readme_autotoc_md23925}
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.


\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_read_cache_node_modules_pify_readme_autotoc_md23926}{}\doxyparagraph{include}\label{md_sfcode_node_modules_read_cache_node_modules_pify_readme_autotoc_md23926}
Type\+: {\ttfamily array} of ({\ttfamily string}$\vert${\ttfamily regex})

Methods in a module to promisify. Remaining methods will be left untouched.\hypertarget{md_sfcode_node_modules_read_cache_node_modules_pify_readme_autotoc_md23927}{}\doxyparagraph{exclude}\label{md_sfcode_node_modules_read_cache_node_modules_pify_readme_autotoc_md23927}
Type\+: {\ttfamily array} of ({\ttfamily string}$\vert${\ttfamily regex}) ~\newline
 Default\+: {\ttfamily \mbox{[}/.+Sync\$/\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_read_cache_node_modules_pify_readme_autotoc_md23928}{}\doxyparagraph{exclude\+Main}\label{md_sfcode_node_modules_read_cache_node_modules_pify_readme_autotoc_md23928}
Type\+: {\ttfamily boolean} ~\newline
 Default\+: {\ttfamily false}

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.


\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(data, null);}
\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_read_cache_node_modules_pify_readme_autotoc_md23929}{}\doxysection{License}\label{md_sfcode_node_modules_read_cache_node_modules_pify_readme_autotoc_md23929}
MIT © \href{http://sindresorhus.com}{\texttt{ Sindre Sorhus}} 