\href{https://travis-ci.org/digitaldesignlabs/es6-promisify}{\texttt{ }}\hypertarget{md_sfcode_node_modules_es6_promisify_README_autotoc_md9171}{}\doxysection{es6-\/promisify}\label{md_sfcode_node_modules_es6_promisify_README_autotoc_md9171}
Converts callback-\/based functions to Promise-\/based functions.\hypertarget{md_sfcode_node_modules_es6_promisify_README_autotoc_md9172}{}\doxysubsection{Install}\label{md_sfcode_node_modules_es6_promisify_README_autotoc_md9172}
Install with \href{https://npmjs.org/package/es6-promisify}{\texttt{ npm}}


\begin{DoxyCode}{0}
\DoxyCodeLine{npm install -\/-\/save es6-\/promisify }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_es6_promisify_README_autotoc_md9173}{}\doxysubsection{Example}\label{md_sfcode_node_modules_es6_promisify_README_autotoc_md9173}

\begin{DoxyCode}{0}
\DoxyCodeLine{"{}use strict"{};}
\DoxyCodeLine{}
\DoxyCodeLine{// Declare variables}
\DoxyCodeLine{const promisify = require("{}es6-\/promisify"{});}
\DoxyCodeLine{const fs = require("{}fs"{});}
\DoxyCodeLine{}
\DoxyCodeLine{// Convert the stat function}
\DoxyCodeLine{const stat = promisify(fs.stat);}
\DoxyCodeLine{}
\DoxyCodeLine{// Now usable as a promise!}
\DoxyCodeLine{stat("{}example.txt"{}).then(function (stats) \{}
\DoxyCodeLine{    console.log("{}Got stats"{}, stats);}
\DoxyCodeLine{\}).catch(function (err) \{}
\DoxyCodeLine{    console.error("{}Yikes!"{}, err);}
\DoxyCodeLine{\}); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_es6_promisify_README_autotoc_md9174}{}\doxysubsection{Promisify methods}\label{md_sfcode_node_modules_es6_promisify_README_autotoc_md9174}

\begin{DoxyCode}{0}
\DoxyCodeLine{"{}use strict"{};}
\DoxyCodeLine{}
\DoxyCodeLine{// Declare variables}
\DoxyCodeLine{const promisify = require("{}es6-\/promisify"{});}
\DoxyCodeLine{const redis = require("{}redis"{}).createClient(6379, "{}localhost"{});}
\DoxyCodeLine{}
\DoxyCodeLine{// Create a promise-\/based version of send\_command}
\DoxyCodeLine{const client = promisify(redis.send\_command, redis);}
\DoxyCodeLine{}
\DoxyCodeLine{// Send commands to redis and get a promise back}
\DoxyCodeLine{client("{}ping"{}).then(function (pong) \{}
\DoxyCodeLine{    console.log("{}Got"{}, pong);}
\DoxyCodeLine{\}).catch(function (err) \{}
\DoxyCodeLine{    console.error("{}Unexpected error"{}, err);}
\DoxyCodeLine{\}).then(function () \{}
\DoxyCodeLine{    redis.quit();}
\DoxyCodeLine{\}); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_es6_promisify_README_autotoc_md9175}{}\doxysubsection{Handle callback multiple arguments}\label{md_sfcode_node_modules_es6_promisify_README_autotoc_md9175}

\begin{DoxyCode}{0}
\DoxyCodeLine{"{}use strict"{};}
\DoxyCodeLine{}
\DoxyCodeLine{// Declare functions}
\DoxyCodeLine{function test(cb) \{}
\DoxyCodeLine{    return cb(undefined, 1, 2, 3);}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{// Declare variables}
\DoxyCodeLine{const promisify = require("{}es6-\/promisify"{});}
\DoxyCodeLine{}
\DoxyCodeLine{// Create promise-\/based version of test}
\DoxyCodeLine{const single = promisify(test);}
\DoxyCodeLine{const multi = promisify(test, \{multiArgs: true\});}
\DoxyCodeLine{}
\DoxyCodeLine{// Discards additional arguments}
\DoxyCodeLine{single().then(function (result) \{}
\DoxyCodeLine{    console.log(result); // 1}
\DoxyCodeLine{\});}
\DoxyCodeLine{}
\DoxyCodeLine{// Returns all arguments as an array}
\DoxyCodeLine{multi().then(function (result) \{}
\DoxyCodeLine{    console.log(result); // [1, 2, 3]}
\DoxyCodeLine{\}); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_es6_promisify_README_autotoc_md9176}{}\doxysubsubsection{Tests}\label{md_sfcode_node_modules_es6_promisify_README_autotoc_md9176}
Test with nodeunit 
\begin{DoxyCode}{0}
\DoxyCodeLine{\$ npm test }

\end{DoxyCode}


Published under the \href{http://opensource.org/licenses/MIT}{\texttt{ MIT License}}. 