.. | ||
test | ||
.npmignore | ||
.travis.yml | ||
index.js | ||
LICENCE | ||
package.json | ||
README.md |
After
Invoke callback after n calls
Status: production ready
Example
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)
.body = body
resultsnext(null, results)
})
getSessionUser(req, res, function (err, user) {
if (err) return next(err)
.user = user
resultsnext(null, results)
})
// now do the thing!
function updateUser(err, result) {
if (err) {
.statusCode = 500
resreturn res.end("Unexpected Error")
}
if (!result.user || result.user.role !== "admin") {
.statusCode = 403
resreturn res.end("Permission Denied")
}
.put("users:" + req.params.userId, result.body, function (err) {
dbif (err) {
.statusCode = 500
resreturn res.end("Unexpected Error")
}
.statusCode = 200
res.end("Ok")
res
})
} }
Naive Example
var after = require("after")
, next = after(3, logItWorks)
next()
next()
next() // it works
function logItWorks() {
console.log("it works!")
}
Example with error handling
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