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

async-pond

v1.0.9

Published

一个使用 promise 来解决并发异步控制的方案,无任何依赖

Downloads

8

Readme

AsyncPond

一个使用 promise 来解决并发异步控制的方案,无任何依赖

安装

npm install async-pond --save

示例

// 一个异步池子,池子里的异步数量控制在3个
const asyncPond = new AsyncPond(3);

// 请求参数数组
const params = [
  {
    id: 1,
    pending: 1000,
  },
  {
    id: 2,
    pending: 9000,
  },
  {
    id: 3,
    pending: 2000,
  },
  {
    id: 4,
    pending: 3000,
  },
  {
    id: 5,
    pending: 1500,
  },
  {
    id: 6,
    pending: 5000,
  },
  {
    id: 7,
    pending: 4000,
  },
];

// 异步生成器
const asyncGenerator = (param) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      console.log(`${param.id} is finish`);
      resolve(param.id);
    }, param.pending);
  });
};

// 往异步池子里push参数和异步生成器
const promise = asyncPond.push(params, asyncGenerator);

promise.then((result) => {
  console.log(result);
});

promise的返回值result打印结果:

- 1 is finish
- 3 is finish
- 5 is finish
- 4 is finish
- 7 is finish
- 6 is finish
- 2 is finish

最后打印:

[
  { "status": "fulfilled", "value": 1 },
  { "status": "fulfilled", "value": 2 },
  { "status": "fulfilled", "value": 3 },
  { "status": "fulfilled", "value": 4 },
  { "status": "fulfilled", "value": 5 },
  { "status": "fulfilled", "value": 6 },
  { "status": "fulfilled", "value": 7 },
];

参数说明

构造函数

new AsyncPond(limit)返回一个可以控制并发的实例;

limit:当前并发的最高数量,默认为:3

// 最高并发数量为 5
const asyncPond = new AsyncPond(5);

属性

push

const asyncPond = new AsyncPond(5);
asyncPond.push(poolParams, iteratorFn);
  • iteratorFn:一个函数,作为异步生成器,返回一个Promise推入异步池子中进行控制;
  • poolParams:一个数组,每一项都将作为iteratorFn的参数

push返回值为一个Promise,其每一项与poolParams参数数组一一对应,返回形如以下的值:

[
  { "status": "fulfilled", "value": "this is success result" },
  { "status": "rejected", "reason": "this is failed result" }
]

并发持续控制

支持在发生并发的同时,不断地往实例池子里进行push操作,以加入当前实例的并发控制:

const asyncPond = new AsyncPond(5);
const push1 = asyncPond.push(poolParams1, iteratorFn);
// push1返回poolParams1一一对应的响应值

setTimeout(() => {
  // 在 push1 还在pending进行并发控制时,可继续往池子里注入新的任务
  const push2 = asyncPond.push(poolParams2, iteratorFn);
  //push2返回poolParams2一一对应的响应值
}, 2000);