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

@mrli-utils/asyncpool

v2.0.0

Published

异步请求并发控制函数,限制同一时间发起的请求量

Downloads

4

Readme

@mrli-utils/asyncpool

适用于浏览器、Nodejs 的javascript 的并发控制函数

1. Installation

# npm
npm install @mrli-utils/asyncpool --save

# yarn
yarn add  @mrli-utils/asyncpool

# pnpm
pnpm install  @mrli-utils/asyncpool

Attribute

asyncPool(limit, paramsArray, serviceFn, [useAllSettled])

asyncPool函数接收四个参数

limit

  • 类型:Number
  • 描述:同一时间发请求的最大数量

paramsArray

  • 类型:any[][]: 二维数组,
  • 描述:若serviceFn接受n个参数, 那么内层的每一个数组都应该有n项元素,n项元素将依次作为serviceFn函数入参的每一项

serviceFn

  • 类型:Function
  • 描述:真正的异步请求函数 若请求函数内部需要绑定this, 需要用bind进行绑定后再传入

useAllSettled(可选)

  • 类型:Boolean
  • 描述:asyncPool内部默认使用 Promise.all处理批量请求, 若useAllSettledtrue, 则内部使用Promise.allSettled处理异步请求,注意: useAllSettled为true时候, 即使serviceFn函数抛出异常, 也不会终止整个asyncPool,直到所有参数都由servicerFn处理完成后才会有最终的返回结果

3. Usage

按需引入

/* Nodejs */
const { asyncPool } = require("@mrli-utils/asyncpool");

/* 浏览器 */
import { asyncPool } from '@mrli-utils/asyncpool'

使用示例

// 真是的请求函数
function getPageList(filter, page) {
 return axios.get('/xxxxx',{
    method:'GET',
    params:{ filter, page }
 })
}

// 每次请求的参数, 有个数组元素就会发出几次请求, 这里是5次
let paramsArray = [
  [{name:'xxx'}, {pageSize:100, pageNum: 1}],
  [{name:'xxx'}, {pageSize:100, pageNum: 2}],
  [{name:'xxx'}, {pageSize:100, pageNum: 3}],
  [{name:'xxx'}, {pageSize:100, pageNum: 4}],
  [{name:'xxx'}, {pageSize:100, pageNum: 5}]
];

// 若请求函数内部需要绑定this, 需要用bind进行绑定后使用
const serviceName = getPageList.bind(this) // 伪代码


// 请求示例, 2表示同一时间内最多2个请求
asyncPool(2, paramsArray, serviceName)
  .then(result => {
    console.log("请求结果数组", result); // result是每个请求返回的结果
    const data = [];  // 最终的结果
    result.forEach(res=>{
      // 将请求结果取出来放到同一个数组内
      data.push(res.xxxxx)
    })

    // ... 对请求结果的后续操作

  })
  .catch(e => {
    // 只要paramsArray.length个请求中有一个出错了, 就会进入catch
    console.log("出错了", e);
  });