Security News
The Dark Side of Open Source
At Node Congress, Socket CEO Feross Aboukhadijeh uncovers the darker aspects of open source, where applications that rely heavily on third-party dependencies can be exploited in supply chain attacks.
Package description
The pend npm package is designed to manage and limit the concurrency of asynchronous operations in Node.js applications. It provides a simple API to queue tasks and control how many of them are executed in parallel, making it easier to manage resources and improve the performance of applications that perform a lot of asynchronous operations, such as file I/O or network requests.
Limiting concurrency of asynchronous operations
This code sample demonstrates how to use pend to limit the concurrency of asynchronous operations. It creates a new Pend instance, sets a concurrency limit, and then queues several asynchronous operations. Pend ensures that only a specified number of operations run in parallel, and provides a callback for when all operations are completed.
const Pend = require('pend');
const pend = new Pend();
pend.max = 2; // Limit to 2 concurrent operations
function asyncOperation(callback) {
setTimeout(() => {
console.log('Operation completed');
callback();
}, 1000);
}
for (let i = 0; i < 5; i++) {
pend.go((cb) => {
asyncOperation(cb);
});
}
pend.wait(() => {
console.log('All operations completed');
});
The async package is a comprehensive collection of utility functions for working with asynchronous JavaScript. It includes features for controlling the flow of asynchronous operations, such as limiting concurrency, similar to pend. However, async offers a broader range of functionalities beyond concurrency control, making it more versatile but also more complex.
p-limit is a lightweight package specifically designed to limit the concurrency of promise-returning & async functions. It is very similar to pend in its purpose but works with Promises instead of callbacks, making it more suitable for modern asynchronous patterns in JavaScript.
Readme
Dead-simple optimistic async helper.
var Pend = require('pend');
var pend = new Pend();
pend.max = 10; // defaults to Infinity
pend.go(function(cb) {
console.log("this function is immediately executed");
setTimeout(function() {
console.log("calling cb 1");
cb();
}, 500);
});
pend.go(function(cb) {
console.log("this function is also immediately executed");
setTimeout(function() {
console.log("calling cb 2");
cb();
}, 1000);
});
pend.wait(function(err) {
console.log("this is excuted when the first 2 have returned.");
console.log("err is a possible error in the standard callback style.");
});
Output:
this function is immediately executed
this function is also immediately executed
calling cb 1
calling cb 2
this is excuted when the first 2 have returned.
err is a possible error in the standard callback style.
FAQs
dead-simple optimistic async helper
The npm package pend receives a total of 14,575,236 weekly downloads. As such, pend popularity was classified as popular.
We found that pend demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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
At Node Congress, Socket CEO Feross Aboukhadijeh uncovers the darker aspects of open source, where applications that rely heavily on third-party dependencies can be exploited in supply chain attacks.
Research
Security News
The Socket Research team found this npm package includes code for collecting sensitive developer information, including your operating system username, Git username, and Git email.
Security News
OpenJS is warning of social engineering takeovers targeting open source projects after receiving a credible attempt on the foundation.