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

limit-promise-all-in-one

v1.0.4

Published

Contains some commonly used methods to limit Promise concurrency

Downloads

11

Readme

limit-promise-all-in-one

Contains some commonly used functions that limit the concurrency of Promises.

中文文档

Why?

Some asynchronous requests may cause some problems if the concurrency is too large. For example, too frequent network requests may exceed the server's access limit. One solution is to limit the maximum number of pending Promises, and redundant pending requests need to be queued for execution. Based on this idea, this library provides some functions to handle such situations.

Install

npm install limit-promise-all-in-one

Usage

First define a function that returns Promise, which will be used in the following examples:

const timeout = (time) =>
  new Promise((resolve) => {
    setTimeout(() => {
      resolve(time);
    }, time);
  });

LimitPromisePool

A concurrent task pool instance used to create a Promise. The instance will provide some functions to limit the number of concurrent Promises passed into it. Its configuration items are as follows:

| Configuration | Type | Remark | | ------------- | ------ | ------------------------------------------- | | max | number | The number of concurrent executions allowed |

LimitPromisePool.call

Execute a function that returns a Promise immediately.

import { LimitPromisePool } from 'limit-promise-all-in-one';

const limitPromise = new LimitPromisePool({ max: 2 });

limitPromise.call(() => timeout(1000)).then((r) => console.log(r));
limitPromise.call(() => timeout(500)).then((r) => console.log(r));
limitPromise.call(() => timeout(300)).then((r) => console.log(r));
limitPromise.call(() => timeout(600)).then((r) => console.log(r));

// Output: 500 300 1000 600

LimitPromisePool.bind

Transform a function that returns a Promise so that the execution of this function is limited by the amount of concurrency.

import { LimitPromisePool } from 'limit-promise-all-in-one';

const limitPromise = new LimitPromisePool({ max: 2 });

const limitTimeout = limitPromise.bind(timeout);

limitTimeout(1000).then((r) => console.log(r));
limitTimeout(500).then((r) => console.log(r));
limitTimeout(300).then((r) => console.log(r));
limitTimeout(600).then((r) => console.log(r));

// Output: 500 300 1000 600

LimitPromiseAll

The usage of this function is similar to Promise.all, except that the Promise function in each array element needs to be wrapped in a function and used as its return value.

import { LimitPromiseAll } from 'limit-promise-all-in-one';

const pList = [
  () => timeout(1000),
  () => timeout(500),
  () => timeout(300),
  () => timeout(600),
];

LimitPromiseAll(pList, 2).then((r) => console.log(r));

// Output: [ 1000, 500, 300, 600 ]