npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dhlx/async-pool

v0.0.7

Published

一个轻量级的 JavaScript 异步任务并发控制工具。它允许你限制同时运行的异步任务数量,确保在同一时间只执行指定数量的任务。

Downloads

212

Readme

Async Pool

一个轻量级的 JavaScript 异步任务并发控制工具。它允许你限制同时运行的异步任务数量,确保在同一时间只执行指定数量的任务。

安装

通过 npm 安装:

npm install async-pool

使用方法

asyncPool 函数可以让你管理异步操作的并发性。你可以限制并行执行的任务数量,并以受控方式处理它们。

示例

以下示例展示了如何使用 asyncPool 限制并发的异步任务。

import asyncPool from 'async-pool';

// 创建一个异步任务
const mockAsyncTask = (id, delay) => {
  return () =>
    new Promise((resolve) => {
      setTimeout(() => {
        resolve(`任务 ${id} 完成`);
      }, delay);
    });
};

// 创建一个并发限制为 3 的任务池
const pool = asyncPool({ limit: 3 });

const tasks = [
  pool(() => mockAsyncTask(1, 500)()),
  pool(() => mockAsyncTask(2, 1000)()),
  pool(() => mockAsyncTask(3, 300)()),
  pool(() => mockAsyncTask(4, 700)()),
  pool(() => mockAsyncTask(5, 200)()),
];

// 开始所有任务
Promise.all(tasks).then((results) => {
  console.log('所有任务已完成: ', results);
});

在上述示例中:

我们创建了几个不同延迟时间的异步任务。 asyncPool 用于限制同时运行的任务数量为 3。 任务分批执行,确保在任何时刻最多只有 3 个任务在运行。

配置

你可以通过传递可选的配置对象来自定义 asyncPool。

const pool = asyncPool({ limit: 5 });

limit:(可选) 最大并发任务数量。默认为 6。

API

asyncPool(config?)
  config:
    limit: (可选) 同时执行的最大任务数,默认为 6。

返回值: 一个函数,该函数接受一个异步任务,并将其加入队列进行执行。

示例:


const pool = asyncPool({ limit: 3 });
pool(() => someAsyncFunction());

测试

单元测试使用 ava 编写。你可以使用以下命令运行测试:

npm test

提供了示例测试用例,以验证并发控制和错误处理功能。

许可证

该项目基于 MIT 许可证开源。