🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

tiny-async-pool

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tiny-async-pool

Run multiple promise-returning & async functions with limited concurrency using native ES9

2.1.0
latest
Source
npm
Version published
Weekly downloads
1.8M
10.83%
Maintainers
1
Weekly downloads
 
Created

What is tiny-async-pool?

The tiny-async-pool npm package is a lightweight utility for managing a pool of asynchronous tasks. It allows you to run a limited number of asynchronous operations concurrently, which can be useful for controlling resource usage and improving performance in scenarios where you have a large number of tasks to process.

What are tiny-async-pool's main functionalities?

Basic Usage

This example demonstrates the basic usage of tiny-async-pool. It runs a pool of asynchronous tasks with a concurrency limit of 2. The `timeout` function simulates an asynchronous operation that resolves after 1 second. The pool processes the array [1, 2, 3, 4, 5] with a concurrency of 2, meaning only 2 tasks will run at the same time.

const asyncPool = require('tiny-async-pool');

const timeout = (i) => new Promise(resolve => setTimeout(() => resolve(i), 1000));

(async () => {
  const results = await asyncPool(2, [1, 2, 3, 4, 5], timeout);
  console.log(results); // [1, 2, 3, 4, 5]
})();

Handling Errors

This example shows how to handle errors in tiny-async-pool. The `faultyTask` function simulates an asynchronous operation that rejects with an error for even numbers. The pool processes the array [1, 2, 3, 4, 5] with a concurrency of 2. If any task fails, the error is caught and logged.

const asyncPool = require('tiny-async-pool');

const faultyTask = (i) => new Promise((resolve, reject) => {
  if (i % 2 === 0) reject(new Error(`Error on ${i}`));
  else resolve(i);
});

(async () => {
  try {
    const results = await asyncPool(2, [1, 2, 3, 4, 5], faultyTask);
    console.log(results);
  } catch (error) {
    console.error(error); // Error on 2
  }
})();

Other packages similar to tiny-async-pool

Keywords

race

FAQs

Package last updated on 10 May 2022

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