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

parallelize-generator-promises

v1.1.5

Published

Runs promises yielded by generators in parallel

Downloads

14

Readme

parallelize-generator-promises

bundle size codecov JSR score JSR npm license

A simple utility to run promises yielded by a generator in parallel. This is incredibly useful when you need to run a bunch of tasks as fast as possible without waiting for one another.

Installation

parallelize-generator-promises is available on both npm and JSR.

To use from npm, install the parallelize-generator-promises package and then import into a module:

import { parallelizeGeneratorPromises } from "parallelize-generator-promises";

To use from JSR, install the @reda/parallelize-generator-promises package and then import into a module:

import { parallelizeGeneratorPromises } from "@reda/parallelize-generator-promises";

Polyfilling Promise.withResolvers

If you are using Node.js v20 or below, you will require a polyfill for Promise.withResolvers.

A polyfill for this is available in a separate export as part of this package. To use the provided polyfill, import it at the entrypoint of your application.

npm:

import "parallelize-generator-promises/promise-with-resolvers-polyfill";

jsr:

import "@reda/parallelize-generator-promises/promise-with-resolvers-polyfill";

Usage

import { parallelizeGeneratorPromises } from "parallelize-generator-promises";

async function* getAllProductDetails() {
  for (let page = 1; page <= 100; page++) {
    const products = await fetch(`/products?page=${page}`).then((res) =>
      res.json()
    );
    yield products.map((product) =>
      fetch(`/product/${product.id}/details`).then((res) => res.json())
    );
  }
}

for await (
  const productDetails of parallelizeGeneratorPromises(
    getAllProductDetails(),
  )
) {
  console.log(productDetails);
}

The above example will fetch all product details for one page of products at a time as fast as possible, yielding the product details in the fastest order possible to keep the iterator fed.

If a concurrency limit is required, this utility pairs very well with a semaphore library such as async-sema.

Options

| Name | Description | | :-------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | maxBufferedPromises | Limits the maximum number of promises that can be buffered at any given time. Useful to manage memory usage in the case where you are generating a lot of promises that aren't being consumed at a fast enough rate. NOTE: this value must be greater than or equal to 1. By default this is undefined which means there is no limit set. |