Security News
Internet Archive Hacked, 31 Million Record Compromised
The Internet Archive's "Wayback Machine" has been hacked and defaced, with 31 millions records compromised.
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.
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);
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.
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'.
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.
Invoke callback after n calls
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")
})
}
}
var after = require("after")
, next = after(3, logItWorks)
next()
next()
next() // it works
function logItWorks() {
console.log("it works!")
}
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)
}
npm install after
npm test
FAQs
after - tiny flow control
We found that after demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The Internet Archive's "Wayback Machine" has been hacked and defaced, with 31 millions records compromised.
Security News
TC39 is meeting in Tokyo this week and they have approved nearly a dozen proposals to advance to the next stages.
Security News
Our threat research team breaks down two malicious npm packages designed to exploit developer trust, steal your data, and destroy data on your machine.