# after

> after - tiny flow control

Latest version **0.8.2** (published 2016-08-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install after
pnpm add after
yarn add after
bun add after
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.8.2 |
| Published | 2016-08-16 |
| First published | 2011-11-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 115 |
| Author | Raynos |
| Maintainers | raynos, defunctzombie |
| Keywords | flowcontrol, after, flow, control, arch |

## Links

- npm: https://www.npmjs.com/package/after
- Repository: https://github.com/Raynos/after
- Homepage: https://github.com/Raynos/after#readme
- Issues: https://github.com/Raynos/after/issues
- npm.io page: https://npm.io/package/after

## Alternatives

- [lodash.startswith](https://npm.io/package/lodash.startswith.md) — 769.7K weekly downloads
- [@tarojs/service](https://npm.io/package/@tarojs/service.md) — 33.9K weekly downloads
- [io.extendreality.tilia.indicators.spatialtargets.unity](https://npm.io/package/io.extendreality.tilia.indicators.spatialtargets.unity.md) — 131 weekly downloads
- [@rtarojs/taro](https://npm.io/package/@rtarojs/taro.md) — 90 weekly downloads
- [node-branch-io](https://npm.io/package/node-branch-io.md) — 50 weekly downloads

## Recent versions

- 0.8.2 (latest) — 2016-08-16
- 0.8.1 — 2013-06-30
- 0.7.0 — 2013-02-26
- 0.6.0 — 2012-07-15
- 0.5.0 — 2012-05-30
- 0.4.1 — 2012-05-08
- 0.4.0 — 2012-04-23
- 0.3.4 — 2012-04-11
- 0.3.3 — 2012-03-04
- 0.3.2 — 2012-02-28
- 0.3.1 — 2012-01-30
- 0.1.0 — 2011-12-08
- 0.1.3 — 2011-12-08
- 0.1.4 — 2011-12-08
- 0.1.5 — 2011-12-08
- … 4 more at https://npm.io/package/after/versions

## README

# After [![Build Status][1]][2]

Invoke callback after n calls

## Status: production ready

## Example

```js
var after = require("after")
var db = require("./db") // some db.

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

## Naive Example

```js
var after = require("after")
    , next = after(3, logItWorks)

next()
next()
next() // it works

function logItWorks() {
    console.log("it works!")
}
```

## Example with error handling

```js
var after = require("after")
    , next = after(3, logError)

next()
next(new Error("oops")) // logs oops
next() // does nothing

// This callback is only called once.
// If there is an error the callback gets called immediately
// this avoids the situation where errors get lost.
function logError(err) {
    console.log(err)
}
```

## Installation

`npm install after`

## Tests

`npm test`

## Contributors

 - Raynos
 - defunctzombie

## MIT Licenced

  [1]: https://secure.travis-ci.org/Raynos/after.png
  [2]: http://travis-ci.org/Raynos/after
  [3]: http://raynos.org/blog/2/Flow-control-in-node.js
  [4]: http://stackoverflow.com/questions/6852059/determining-the-end-of-asynchronous-operations-javascript/6852307#6852307
  [5]: http://stackoverflow.com/questions/6869872/in-javascript-what-are-best-practices-for-executing-multiple-asynchronous-functi/6870031#6870031
  [6]: http://stackoverflow.com/questions/6864397/javascript-performance-long-running-tasks/6889419#6889419
  [7]: http://stackoverflow.com/questions/6597493/synchronous-database-queries-with-node-js/6620091#6620091
  [8]: http://github.com/Raynos/iterators
  [9]: http://github.com/Raynos/composite

---
_Source: https://npm.io/package/after · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
