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

capped-promise

v1.0.4

Published

Provides Promise.all and Promise.allSettled variants that limit the number of simultaneously pending promises.

Downloads

11

Readme

Provides Promise.all and Promise.allSettled variants that limit the number of simultaneously pending promises. This is useful e.g. when you need to make thousands of requests to a single server but do not want to hit it with all requests at once. Instead you might want to have at most 10 requests pending simultaneously and whenever one settles, the next one is initiated automatically.

This is a ES2019 CommonJS module, that works exactly the same way in CommonJS and ES modules (because there's only an unnamed default export). TypeScript typings are provided.

Installation

npm install capped-promise

Example

// Only necessary in node, fetch is natively available in a browser.
import fetch from "node-fetch";
import CappedPromise from "capped-promise";

const getText = async (url: string) => {
    const response = await fetch(url);

    if (!response.ok) {
        throw new Error("Unexpected response");
    }

    return await response.text();
};

const cssUrls = [
    "https://fonts.googleapis.com/css2?family=Roboto",
    "https://fonts.googleapis.com/css2?family=Open+Sans",
    "https://fonts.googleapis.com/css2?family=Poppins",
];

// Promise.all
// We pass already pending promises.
const cssPromises = cssUrls.map((url) => getText(url));
// All requests are made at the same time.
const promiseResults = await Promise.all(cssPromises);

// CappedPromise.all
// We pass promise-creating functions instead of promises.
const createCssPromises = cssUrls.map((url) => () => getText(url));
// We allow at most 2 simultaneous requests. So, in the beginning the first
// two promises are created right away. As soon as one of those is fulfilled,
// the third is automatically created. When the responses for all requests are
// in, the returned promise fulfills.
const cappedResults = await CappedPromise.all(2, createCssPromises);

Interface

Promise.all and Promise.allSettled accept iterables of awaitables (usually promises). By its very nature, once a promise has been created, it is in the state pending. That is, the underlying operation is already running. So, if you create e.g. 1000 Promise objects by calling fetch() repeatedly (without awaiting them), all those requests will be made quasi simultaneously, if you pass them to Promise.all and then await the result. This is why CappedPromise.all and CappedPromise.allSettled differ from their Promise counterparts as follows:

  • Instead of awaitables, they accept parameterless functions that are expected to create and return awaitables. The CappedPromise implementation will then call these functions to create new awaitables as necessary.
  • The first parameter is maxPending, which specifies how many promises can at most be pending simultaneously.

Otherwise, CappedPromise.all and CappedPromise.allSettled follow their Promise counterparts.

Behavior

Again, the CappedPromise behavior follows the Promise behavior as much as possible. However, due to the different interface there is the following additional concern: If a function passed to a CappedPromise method throws right away, e.g. () => { throw new Error("Oops!"); } then this is treated as an unintended catastrophic failure and is propagated as a rejection immediately, even if the function was passed to allSettled. If this is not what you expect, you should return a rejecting Promise, as follows: () => Promise.reject(new Error("Oops!")).