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

concurrency-request

v1.0.91

Published

用于分组发起并行请求

Downloads

4

Readme

Description

concurrency-request, it can split multiple requests simultaneously and group them together. You can use it when you need to send a large number of requests and don't care about the order in which they are completed.

concurrency-request,它可以同时将多个请求项进行拆分,并分组请求。你可以在需要发送大量请求且不在乎请求完成的先后顺序时使用它。

Versions

| Version | Date | Change logs(CN) | Change logs(EN) | | :-----: | :----------: | :----------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------ | | 1.0.0 | 2022-09-28 | 初始化版本 | Initialize the version | | 1.0.1 | 2022-09-29 | 新增了 setOnRequest,移除 setIgnoreStatusCodeError | Added 'setOnRequest', removed 'setIgnoreStatusCodeError' | | 1.0.9 | 2022-09-30 | request 替换为 axios 的过度版本,此时还是使用的request,但setOnRequest的参数已经发生改变 | 'request' is replaced with an overdone version of axios. Request is still used, but the parameters of setOnRequest have changed | | *1.0.91 | 2022-10-31 | onGroup回调新增progress参数,用于知晓进度 | Added 'progress' on 'OnGroup'|

Usage

npm i concurrency-request
const ConcurrencyRequest = require("concurrency-request");
// create options
const options = new Array(100).fill({
    method: "GET",
    url: "https://www.example.com",
    headers: {},
});
// instantiation concurrency-request and run
// the default group is 20 requets
new ConcurrencyRequest(options).run().then((allResult) => {
    allResult = allResult
        .filter(([error, result]) => !error)
        .map(([error, result]) => result);
});

options参数说明:version < '1.1.0'? 请参考request(config): 请参考axios(config)

In addition, you can carry other properties that are not used by option, but are also available in the onRequest after the request is completed. As follows:

除此之外你还可以携带其他不被 option 使用的属性,携带的属性在请求结束之后的onRequest中同样能获取到。如下:

const options = new Array(100).fill({
    method: "GET",
    url: "https://www.example.com",
    headers: {},
    _row: {
        name: "Erick",
        car: "BMW",
    },
});
const onRequest = function (error, respones) {
    const data = respones.data,
        /* _row is additional value*/
        _row = respones.config._row;
    // ...
};
await new ConcurrencyRequest(options).group(10).setOnRequest(onRequest).run();

API


group

Group options requests, groupGount is the maximum number of each group, groupGount default value is 20.

options 请求进行分组, groupGount 为每组的最大数量, 默认值为 20

await new ConcurrencyRequest(options).group(10).run();

setGroupSleepTime

Sets the latency, in milliseconds, for completing a set of requests

设置完成一组请求的延迟时间,单位毫秒

await new ConcurrencyRequest(options).setGroupSleepTime(300).run();

setOnRequest

When you need to know or process the request when it is completed. You can use setOnRequest:

It takes a function as an argument. This function will have the error, respones attributes used in the callback. Note that you need to return a value inside the function to be used as the return value of the request result.

当你需要在请求完成时知晓或处理的情况下。你可以使用 setOnRequest:

它接受一个函数作为参数。该函数回调时会有 error, respones参数提供。值得注意的是,你需要在该函数内部返回一个值,作为该请求的返回值使用。

// If you need to do something with the requested data
const options = new Array(10).fill({
    method: "GET",
    url: "https://www.example.com",
    headers: {},
    _row: {
        name: "Erick",
        car: "BMW",
    },
});
const onRequest = function (error, respones) {
    const { statusCode, data, config } = respones;
    return error
        ? ""
        : {
              name: config._row.name,
              car: config._row.car,
              data: data.slice(0, 10),
          };
};
await new ConcurrencyRequest(options).setOnRequest(onRequest).run();

setRequestErrorCallBack

If you need to know about or handle errors in the function that is passed in when an error occurs in the request, you can do this:

当请求发生错误时会触发传入的函数,若您需要知晓或者处理这些错误,你可以这样做:

const requestError = function (error) {
    // ...
};
await new ConcurrencyRequest(options)
    .setRequestErrorCallBack(requestError)
    .run();

setOnGroup

Sets the callback function that fires when a set of requests completes

设置当一组请求完成时触发的回调函数

const onGroup = function (groupResults, progress) {
    const { total, current } = progress;
    // ...
    groupResults.map(([error, result]) => result);
};
new ConcurrencyRequest(options).setOnGroup(onGroup).run();

run

The group request begins, and the method is called to return a Promise result containing all of the request return information.

开始进行分组请求,调用该方法之后会返回一个Promise结果,该结果中包含了所有的请求返回信息。

// `allResult` data structure like
// [[error, respones | onRequest-returns], ...]
new ConcurrencyRequest(options)
    .run()
    .then(allResult => {
        allResult.map(([error, result] => result))
    })
// or use async/await
const allResult = new ConcurrencyRequest(options).run();
allResult.map(([error, result] => result))