Invoke callback after n calls\hypertarget{md_sfcode_node_modules_after_README_autotoc_md2538}{}\doxysection{Status\+: production ready}\label{md_sfcode_node_modules_after_README_autotoc_md2538}
\hypertarget{md_sfcode_node_modules_after_README_autotoc_md2539}{}\doxysection{Example}\label{md_sfcode_node_modules_after_README_autotoc_md2539}

\begin{DoxyCode}{0}
\DoxyCodeLine{var after = require("{}after"{})}
\DoxyCodeLine{var db = require("{}./db"{}) // some db.}
\DoxyCodeLine{}
\DoxyCodeLine{var updateUser = function (req, res) \{}
\DoxyCodeLine{  // use after to run two tasks in parallel,}
\DoxyCodeLine{  // namely get request body and get session}
\DoxyCodeLine{  // then run updateUser with the results}
\DoxyCodeLine{  var next = after(2, updateUser)}
\DoxyCodeLine{  var results = \{\}}
\DoxyCodeLine{  }
\DoxyCodeLine{  getJSONBody(req, res, function (err, body) \{}
\DoxyCodeLine{    if (err) return next(err)}
\DoxyCodeLine{    }
\DoxyCodeLine{    results.body = body}
\DoxyCodeLine{    next(null, results)}
\DoxyCodeLine{  \})}
\DoxyCodeLine{  }
\DoxyCodeLine{  getSessionUser(req, res, function (err, user) \{}
\DoxyCodeLine{    if (err) return next(err)}
\DoxyCodeLine{    }
\DoxyCodeLine{    results.user = user}
\DoxyCodeLine{    next(null, results)}
\DoxyCodeLine{  \})}
\DoxyCodeLine{  }
\DoxyCodeLine{  // now do the thing!}
\DoxyCodeLine{  function updateUser(err, result) \{}
\DoxyCodeLine{    if (err) \{}
\DoxyCodeLine{      res.statusCode = 500}
\DoxyCodeLine{      return res.end("{}Unexpected Error"{})}
\DoxyCodeLine{    \}}
\DoxyCodeLine{    }
\DoxyCodeLine{    if (!result.user || result.user.role !== "{}admin"{}) \{}
\DoxyCodeLine{      res.statusCode = 403}
\DoxyCodeLine{      return res.end("{}Permission Denied"{})}
\DoxyCodeLine{    \}}
\DoxyCodeLine{    }
\DoxyCodeLine{    db.put("{}users:"{} + req.params.userId, result.body, function (err) \{}
\DoxyCodeLine{      if (err) \{}
\DoxyCodeLine{        res.statusCode = 500}
\DoxyCodeLine{        return res.end("{}Unexpected Error"{})}
\DoxyCodeLine{      \}}
\DoxyCodeLine{      }
\DoxyCodeLine{      res.statusCode = 200}
\DoxyCodeLine{      res.end("{}Ok"{})  }
\DoxyCodeLine{    \})   }
\DoxyCodeLine{  \}}
\DoxyCodeLine{\} }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_after_README_autotoc_md2540}{}\doxysection{Naive Example}\label{md_sfcode_node_modules_after_README_autotoc_md2540}

\begin{DoxyCode}{0}
\DoxyCodeLine{var after = require("{}after"{})}
\DoxyCodeLine{    , next = after(3, logItWorks)}
\DoxyCodeLine{}
\DoxyCodeLine{next()}
\DoxyCodeLine{next()}
\DoxyCodeLine{next() // it works}
\DoxyCodeLine{}
\DoxyCodeLine{function logItWorks() \{}
\DoxyCodeLine{    console.log("{}it works!"{})}
\DoxyCodeLine{\} }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_after_README_autotoc_md2541}{}\doxysection{Example with error handling}\label{md_sfcode_node_modules_after_README_autotoc_md2541}

\begin{DoxyCode}{0}
\DoxyCodeLine{var after = require("{}after"{})}
\DoxyCodeLine{    , next = after(3, logError)}
\DoxyCodeLine{}
\DoxyCodeLine{next()}
\DoxyCodeLine{next(new Error("{}oops"{})) // logs oops}
\DoxyCodeLine{next() // does nothing}
\DoxyCodeLine{}
\DoxyCodeLine{// This callback is only called once.}
\DoxyCodeLine{// If there is an error the callback gets called immediately}
\DoxyCodeLine{// this avoids the situation where errors get lost.}
\DoxyCodeLine{function logError(err) \{}
\DoxyCodeLine{    console.log(err)}
\DoxyCodeLine{\} }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_after_README_autotoc_md2542}{}\doxysection{Installation}\label{md_sfcode_node_modules_after_README_autotoc_md2542}
{\ttfamily npm install after}\hypertarget{md_sfcode_node_modules_after_README_autotoc_md2543}{}\doxysection{Tests}\label{md_sfcode_node_modules_after_README_autotoc_md2543}
{\ttfamily npm test}\hypertarget{md_sfcode_node_modules_after_README_autotoc_md2544}{}\doxysection{Contributors}\label{md_sfcode_node_modules_after_README_autotoc_md2544}

\begin{DoxyItemize}
\item Raynos
\item defunctzombie
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_after_README_autotoc_md2545}{}\doxysection{MIT Licenced}\label{md_sfcode_node_modules_after_README_autotoc_md2545}
