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

directly

v2.0.6

Published

Like Promise.all, only less so

Downloads

1,464

Readme

directly

Like Promise.all, only less so

This module could more descriptively be named Promise.allButNotAllAtOnce. It takes an array of functions, each of which return a promise, and returns a promise which resolves once all those promises have resolved, or otherwise rejects... very similar to Promise.all. The difference is that a maximum of n promises are created at any one time. This is useful for rate-limiting asynchronous calls (e.g. fetch, mongoose...)

*** New feature *** Now supports throttling of potentially infinite queues of Promises (see notes on the Queue class below)

About the name

In the West Country people will often promise to do things 'directly' [drekt-lee], meaning they'll do it when they're good and ready, possibly never. Example usage:

I'll wash the dishes directly, my lover

Usage

const directly = require('directly');
const urls = []; // a big array of urls
const fetchers = urls.map(function (url) {
    return function () {
        return fetch(url);
    }
});


directly(10, fetchers)
    .then(function (results) {
        // handle exactly as if it was a Promise.all()
    });

Can also be called as a constructor (in which case the .run() method should be used)

const Directly = require('Directly');
const throttledRequests = new Directly(10, fetchers)

throttledRequests
    .run()
    .then(function (results) {
        // handle exactly as if it was a Promise.all()
    })

 // can be used to stop the directly instance prematurely
throttledRequests.terminate()

To handle an infinite queue of promises use the Queue class to wrap your array of functions

fetchers = new directly.Queue(fetchers);
directly(10, fetchers)
    .catch(function (errorObject) {
        // You can handle any errors in here
        // The error object has 3 properties
        //  error: The error thrown
        //  nextError: A promise which will reject the next time an error is encountered
        //  terminate: A function to call which will terminate the directly instance
    });

// use push to add to the execution queue. Will work even if the queue has fallen idle
fetchers.push(func1, func2, func3)

Based on an idea originally developed at the FT