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

ajax-limiter

v1.0.3

Published

Controls the maximum number of concurrent requests

Downloads

5

Readme

ajax-limiter

控制并发请求的最大数量。

有别于Promise.all,limiter中请求发送的形式类似于滑动窗口,可以在一个请求成功或失败后,立即对结果进行处理。这可以保证请求在规定的并发限制内(默认为10个)尽快完成,且无需等到请求完成就能对已经完成的请求做处理。

适用于,当你想要尽快完成所有请求,又想控制并发数量的时候。如浏览器中,你有500个请求需要发送,假设因为某些原因,同时发起的请求不能超过十个,否则服务器,或浏览器就会遇到错误,就很适合这种场景。

Node环境同样适用。

Installation

npm i --save ajax-limiter

Usage

const limiter = require('ajax-limiter')
const axios = require('axios') // 发送请求

const requests = [] // 返回promise的函数的数组
for (let i = 0; i < 100; i ++) {
  requests.push(() => axios.get('http://localhost:8080'))
}

limiter(requests, {
  limit: 10, // 并发数量
  exitWhenError: false, 
  success (res, index) {},
  error (e, index) {}
}).then(data => {
  console.log(data) // 按顺序排列的结果
}).catch(e => {
  console.error(e) // 只有 exitWhenError: true 时才可能会执行到这里
})

Configuration

类型声明如下:

type FuncArrWithPromise = Array<() => Promise<any>>

interface Config {
  limit?: number;
  exitWhenError?: boolean;
  success?: (data: any, index: number) => any;
  error?: (e: Error, index: number) => any;
}

function limiter(arr: FuncArrWithPromise, config?: Config): Promise<any>

arr (required)

数组成员为函数,每个函数都返回一个Promise对象,如果返回值不是Promise对象,默认将被resolve

config (optional)

| 字段 | 类型 | 默认值 | 说明 | |-----|-------|:------:|-----| | limit | Number | 10 | 最大并发数量,也就是滑动窗口的最大值 | | exitWhenError | Boolean | false | 请求失败时退出,类似于Promise.all,默认不会退出 | | success | Function, 参数(res, index) | 无 | 单个请求成功时的回调函数,返回值(如果有的话)将存入最终的结果数组 | | error | Function, 参数(err, index) | 无 | 单个请求失败时的回调函数,返回值(如果有的话)将存入最终的结果数组 |

Addtional Notes

测试覆盖率 100%,具体参见src/test目录和package.json