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

promax

v3.1.0

Published

A promise limiting/concurrency library that allows you to control how many promises are run at any given time.

Downloads

1,018

Readme

promax

NPM

GitHub version npm version Build Status

Another promise limiter (adding concurrency to promises). This library uses rxjs to control concurrency, it also adds a little bit of extra functionality around how you get the results.

Setup Instructions

To install this package, run,

npm install --save promax

Usage Instructions

Once it's installed, you can use Promax to run your promises with a specified concurrency value.

Assume we have the following promise function:

function somePromiseFunction(returns = null, timeout = 0) {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(returns);
    }, timeout);
  });
}

Promax is able to run this function concurrently in multiple ways:

import { Promax } from 'promax';

async function run() {
    const limit = 2;
    const expectedValues = [1, 2, 3];
    const promax = Promax.create(limit).add(
        expectedValues.map(ev => () => somePromiseFunction(ev))
    );
    const results = await promax.run();
    // In this case, results == expectedValues
    
    const resultMap = promax.getResultMap();
    /**
     * This would return:
     * {
     *    valid: [1, 2, 3],
     *    error: []
     * }
     */
}

NOTE: In this case, if a promise is rejected, it'll throw an error and end the call.

If we don't want to throw an error, we can pass a parameter object with throws: false to the create function:

import { Promax } from 'promax';

async function run() {
    const limit = 2;
    const expectedValues = [1, 2, 3];
    const promax = Promax.create(limit, { throws: false }).add(
        expectedValues.map(ev => () => somePromiseFunction(ev))
    );
    // ... same as previous example
}

When it doesn't throw, it wraps errored results in an ErrorResult instance. Using instanceof ErrorResult will tell you whether or not there was an error in your array of results. Let's assume we have the following promise function which rejects:

function someFailingPromiseFunction(returns = 0, timeout = 0) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            reject(returns);
        }, timeout);
    });
}

Then we may get the following:

import { Promax } from 'promax';

async function run() {
    const limit = 2;
    const promax = Promax.create(limit, { throws: false }).add([
        () => somePromiseFunction(1),
        () => someFailingPromiseFunction(2),
        () => somePromiseFunction(3),
    ]);
    const results = await promax.run();
    /**
     * Now we would have the following
     * results = [
     *    1,
     *    ErrorResult,
     *    2
     *  ]
     *
     * So:
     * results[0] === 1
     * results[1] instanceof ErrorResult
     * results[1].data === 2
     * results[2] === 3
     */
    
    const resultMap = promax.getResultMap();
    /**
     * This would return:
     * {
     *    valid: [1, 3],
     *    error: [ErrorResult]
     * }
     */
}

IMPORTANT: If you don't need the functionality above, you can chain everything into one call:

import { Promax } from 'promax';

async function run() {
    const limit = 2;
    const expectedValues = [1, 2, 3];
    const results = Promax.create(limit).add(
        expectedValues.map(ev => () => somePromiseFunction(ev))
    ).run();
}

And you can send in the result map by reference:

import { Promax } from 'promax';

async function run() {
    const limit = 2;
    const expectedValues = [1, 2, 3];
    const resultMap = { valid: [], error: [] };
    const results = Promax.create(limit)
        .setResultMapOutput(resultMap)
        .add(
            expectedValues.map(ev => () => somePromiseFunction(ev))
        ).run();
    /**
     * In this case:
     *
     * results = [1, 2, 3]
     * resultMap = {
     *    valid: [1, 2, 3],
     *    error: []
     * }
     */

}