Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
ajax-limiter
Advanced tools
Readme
控制并发请求的最大数量。
有别于Promise.all
,limiter中请求发送的形式类似于滑动窗口,可以在一个请求成功或失败后,立即对结果进行处理。这可以保证请求在规定的并发限制内(默认为10个)尽快完成,且无需等到请求完成就能对已经完成的请求做处理。
适用于,当你想要尽快完成所有请求,又想控制并发数量的时候。如浏览器中,你有500个请求需要发送,假设因为某些原因,同时发起的请求不能超过十个,否则服务器,或浏览器就会遇到错误,就很适合这种场景。
Node
环境同样适用。
npm i --save ajax-limiter
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 时才可能会执行到这里
})
类型声明如下:
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>
数组成员为函数,每个函数都返回一个Promise对象,如果返回值不是Promise对象,默认将被resolve
字段 | 类型 | 默认值 | 说明 |
---|---|---|---|
limit | Number | 10 | 最大并发数量,也就是滑动窗口的最大值 |
exitWhenError | Boolean | false | 请求失败时退出,类似于Promise.all ,默认不会退出 |
success | Function, 参数(res, index) | 无 | 单个请求成功时的回调函数,返回值(如果有的话)将存入最终的结果数组 |
error | Function, 参数(err, index) | 无 | 单个请求失败时的回调函数,返回值(如果有的话)将存入最终的结果数组 |
测试覆盖率 100%,具体参见src/test
目录和package.json
FAQs
Controls the maximum number of concurrent requests
The npm package ajax-limiter receives a total of 7 weekly downloads. As such, ajax-limiter popularity was classified as not popular.
We found that ajax-limiter demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.