What is after?
The 'after' npm package is a small utility that helps manage asynchronous operations by executing a callback after a specified number of operations have completed. It is particularly useful for scenarios where you need to wait for multiple asynchronous tasks to finish before proceeding.
What are after's main functionalities?
Execute callback after multiple asynchronous operations
This feature allows you to specify a callback function that will be executed after a certain number of asynchronous operations have completed. In this example, the callback will be executed after three tasks have finished.
const after = require('after');
const done = after(3, () => {
console.log('All tasks completed');
});
setTimeout(() => {
console.log('Task 1 done');
done();
}, 1000);
setTimeout(() => {
console.log('Task 2 done');
done();
}, 2000);
setTimeout(() => {
console.log('Task 3 done');
done();
}, 3000);
Other packages similar to after
async
The 'async' package provides a wide range of utilities for working with asynchronous JavaScript, including functions for parallel and series execution, queue management, and more. It is more feature-rich compared to 'after' and can handle more complex asynchronous workflows.
promise
The 'promise' package is a lightweight implementation of Promises/A+ that allows you to work with asynchronous operations in a more modern and flexible way compared to callbacks. It provides methods like 'all' and 'race' to manage multiple promises, making it a more versatile option than 'after'.
bluebird
The 'bluebird' package is a fully-featured Promise library that offers advanced features like cancellation, iteration methods, and more. It is more powerful and flexible than 'after', making it suitable for complex asynchronous workflows.
After 
Invoke callback after n calls
Status: production ready
Example
var after = require("after")
var db = require("./db")
var updateUser = function (req, res) {
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)
})
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
var after = require("after")
, next = after(3, logItWorks)
next()
next()
next()
function logItWorks() {
console.log("it works!")
}
Example with error handling
var after = require("after")
, next = after(3, logError)
next()
next(new Error("oops"))
next()
function logError(err) {
console.log(err)
}
Installation
npm install after
Tests
npm test
Contributors
MIT Licenced