Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
promise-batcher
Advanced tools
Changelog
1.1.1 (2023-01-16)
Readme
A module for batching individual promises to improve their collective efficiency.
For release notes, see the changelog.
npm install promise-batcher
Promise batcher is useful for batching requests made from branching execution paths.
This example demonstrates a batcher which takes numbers as inputs, adds 1 to each and returns the result. Note that while the getResult function is called multiple times, the batching function is only run once. If the send method is not called, the batch will still be processed when Node.js idles.
const { Batcher } = require("promise-batcher");
let runCount = 0;
const batcher = new Batcher({
batchingFunction: (nums) => {
runCount++;
return Promise.resolve(
nums.map((n) => {
return n + 1;
}),
);
},
});
// Send the batch of requests. This step is optional.
batcher.send();
const inputs = [1, 3, 5, 7];
// Start a series of individual requests
const promises = inputs.map((input) => batcher.getResult(input));
// Wait for all the requests to complete
Promise.all(promises).then((results) => {
console.log(results); // [ 2, 4, 6, 8 ]
// The requests were still done in a single run
console.log(runCount); // 1
});
This example shows how a batcher could be used to combine individual requests made to a database. This is especially useful when the requests need to be made independently of each other, allowing the requests to be made individually, yet be processed as batches. Note that in a real-world example, it would be best to implement exponential backoff for retried requests by using a delayFunction.
const { Batcher, BATCHER_RETRY_TOKEN } = require("promise-batcher");
const batcher = new Batcher({
batchingFunction: (recordIds) => {
// Perform a batched request to the database with the inputs from getResult()
return database.batchGet(recordIds).then((results) => {
// Interpret the results from the request, returning an array of result values
return results.map((result) => {
if (result.error) {
if (result.error.retryable) {
// Retry the individual request (eg. request throttled)
return BATCHER_RETRY_TOKEN;
} else {
// Reject the individual request (eg. record not found)
return new Error(result.error.message);
}
} else {
// Resolve the individual request
return result.data;
}
});
});
},
});
// Send the batch of requests. This step is optional.
batcher.send();
const recordIds = ["ABC123", "DEF456", "HIJ789"];
// Start a series of individual requests
const promises = recordIds.map((recordId) => batcher.getResult(recordId));
// Wait for all the requests to complete
Promise.all(promises).then((results) => {
// Use the results
});
A tool for combining requests.
new Batcher(options) - Creates a new batcher.
true
when there are no pending batches, and false
otherwise.FAQs
A module for batching individual promises to improve their collective efficiency.
The npm package promise-batcher receives a total of 41,969 weekly downloads. As such, promise-batcher popularity was classified as popular.
We found that promise-batcher 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
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.