Socket
Socket
Sign inDemoInstall

map-limit

Package Overview
Dependencies
1
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

map-limit


Version published
Maintainers
1
Created

Package description

What is map-limit?

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.

What are map-limit's main functionalities?

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);
  }
});

Other packages similar to map-limit

Readme

Source

map-limit Flattr this!experimental

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.

Usage

map-limit

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.

Arguments
  • arr - An array to iterate over.
  • limit - The maximum number of iterators to run at any time.
  • iterator(item, callback) - A function to apply to each item in the array. The iterator is passed a callback(err, transformed) which must be called once it has completed with an error (which can be null) and a transformed item.
  • callback(err, results) - A callback which is called after all the iterator functions have finished, or an error has occurred. Results is an array of the transformed items from the original array.

License

MIT. See LICENSE.md for details.

Keywords

FAQs

Last updated on 07 Aug 2014

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc