Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
The map-limit npm package allows you to map over an array of items with a limit on the number of concurrent asynchronous operations. This is useful for controlling the rate of operations, especially when dealing with APIs or other resources that have rate limits.
Basic Usage
This example demonstrates the basic usage of map-limit. It processes an array of numbers, doubling each one, but only allows two asynchronous operations to run concurrently.
const mapLimit = require('map-limit');
const tasks = [1, 2, 3, 4, 5];
const limit = 2;
function asyncTask(item, callback) {
setTimeout(() => {
callback(null, item * 2);
}, 1000);
}
mapLimit(tasks, limit, asyncTask, (err, results) => {
if (err) throw err;
console.log(results); // [2, 4, 6, 8, 10]
});
Error Handling
This example shows how to handle errors in map-limit. If an error occurs during the processing of an item, it is passed to the final callback.
const mapLimit = require('map-limit');
const tasks = [1, 2, 3, 4, 5];
const limit = 2;
function asyncTask(item, callback) {
setTimeout(() => {
if (item === 3) {
callback(new Error('An error occurred'));
} else {
callback(null, item * 2);
}
}, 1000);
}
mapLimit(tasks, limit, asyncTask, (err, results) => {
if (err) {
console.error(err.message); // 'An error occurred'
} else {
console.log(results);
}
});
The async package provides a wide range of asynchronous utilities, including the mapLimit function, which offers similar functionality to map-limit. It is more feature-rich and widely used, making it a good alternative if you need more than just map limiting.
The p-limit package allows you to run multiple promise-returning & async functions with a concurrency limit. It is more modern and uses promises, making it a good choice for projects that prefer promise-based APIs over callback-based ones.
Bluebird is a fully-featured promise library with a focus on performance and innovative features. It includes a map function with concurrency control, similar to map-limit, but also offers a wide range of other utilities for working with promises.
async.mapLimit's functionality available as a standalone npm module.
I often find myself pulling in async for this method alone, so in the spirit of breaking things into smaller pieces here's that method as a single thing you can require.
mapLimit(arr, limit, iterator, callback)
The same as map only no more than "limit" iterators will be simultaneously running at any time.
Note that the items are not processed in batches, so there is no guarantee that the first "limit" iterator functions will complete before any others are started.
MIT. See LICENSE.md for details.
FAQs
async.mapLimit's functionality available as a standalone npm module
The npm package map-limit receives a total of 233,325 weekly downloads. As such, map-limit popularity was classified as popular.
We found that map-limit 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
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.