Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

p-map

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

p-map

Map over promises concurrently

  • 7.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
37M
decreased by-5.08%
Maintainers
1
Weekly downloads
 
Created

What is p-map?

The p-map npm package is a library that allows you to map over promises concurrently, controlling the number of promises that are running at any given time. It is useful for throttling asynchronous operations that are initiated in a loop, such as API calls, file operations, or any task that returns a promise.

What are p-map's main functionalities?

Concurrency Control

This feature allows you to control the number of promises that are executed concurrently. In the code sample, `pMap` is used to fetch a list of URLs with a concurrency limit of 2, meaning only 2 promises will be running at the same time.

const pMap = require('p-map');

const urls = [/* ... */];
const fetchUrl = async url => {/* ... */};

(async () => {
  const result = await pMap(urls, fetchUrl, {concurrency: 2});
  console.log(result);
})();

Error Handling

p-map provides options for error handling. In this example, `stopOnError` is set to false, which means that p-map will not stop mapping over the remaining items when one promise rejects. Instead, it will continue with the rest and collect all the errors.

const pMap = require('p-map');

const tasks = [/* ... */];
const doTask = async task => {/* ... */};

(async () => {
  try {
    const result = await pMap(tasks, doTask, {concurrency: 4, stopOnError: false});
    console.log(result);
  } catch (error) {
    console.error('An error occurred:', error);
  }
})();

Promise Cancellation

p-map supports promise cancellation when used with cancelable promises, such as those created with the p-cancelable package. This allows you to cancel the execution of the promises if needed.

const pMap = require('p-map');
const {CancelablePromise} = require('p-cancelable');

const tasks = [/* ... */];
const cancellableTask = task => new CancelablePromise(resolve => {/* ... */});

(async () => {
  const mapper = cancellableTask;
  const result = await pMap(tasks, mapper, {concurrency: 3});
  console.log(result);
})();

Other packages similar to p-map

Keywords

FAQs

Package last updated on 03 Apr 2024

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc