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

promise-queue-manager

v3.0.4

Published

A queue manager for concurrent promise execution

Downloads

2,310

Readme

promise-queue-manager

npm version install size language npm

A queue manager for concurrent promise execution

Installation

npm i -S promise-queue-manager

Why use this module?

Sometimes you have to do any large processing using a promise list and you don't want to Promise.all then because it will load all the promises into memory and will stop when any error occur. This package can help you with that! You can specify concurrence and set if it can continue processing even if any error occur. It has zero external dependencies and uses EventEmitter to control event flow.

Upgrading

From 2.x.x to 3.x.x

  • Old constructor parameters concurrence and shouldStopOnError are now passed in config object.
  • The promises list parameter now require a function that returns a promise to avoid early promise execution. (Huge thanks to @dArignac with this issue)

From 1.x.x to 2.x.x

  • PromiseQueue.EVENTS.QUEUE_PROCESSED is now fired even if shouldStopOnError is set to true.

Usage

Demo

You can access a repl demo here

Setup

You can use this lib in two ways: with a list of functions that return a promise or with a promise and a list of items to process. In both cases:

const saveOnDatabase = async (data) => {
    const result = await repository.save(data);

    return result;
};

const config = {
    concurrence: 10,
    shouldStopOnError: true,
};

Using a list of promises:

const items = [
    { name: 'foo' },
    { name: 'bar' },
];

// you need to wrap your promise inside a function
// to avoid early calls
config.promises = items.map(item => () => saveOnDatabase(item));

Using a list of items:

const items = [
    { name: 'foo' },
    { name: 'bar' },
];

config.promise = saveOnDatabase;
config.items = items;

Now you can initialize the queue:

const queue = new PromiseQueue<YourInterface>(config);

Listening

Now you can setup your listeners. The PromiseQueue class have a static enum that helps you setting up your listeners: ITEM_ERROR, ITEM_PROCESSING, ITEM_PROCESSED and QUEUE_PROCESSED, it stays in PromiseQueue.EVENTS.

queue.on(PromiseQueue.EVENTS.ITEM_ERROR, (response: PromiseQueueItemResponse<any>) => {
    console.error(response);

    // you can manually stop the queue calling `.cancel` method
    queue.cancel();
});

// useful only if `items` is used
queue.on(PromiseQueue.EVENTS.ITEM_PROCESSING, (response: PromiseQueueItemResponse<any>) => {
    console.log(response);
});

queue.on(PromiseQueue.EVENTS.ITEM_PROCESSED, (response: PromiseQueueItemResponse<any>) => {
    console.log(response);

    // you can set some rule to cancel the queue anytime you want
    const canContinue = someMethod();
    if (!canContinue) queue.cancel();
});

queue.on(PromiseQueue.EVENTS.QUEUE_PROCESSED, () => {
    console.log(`Done!`);
});

Now you can start the queue:

queue.start();

License

promise-queue-manager is freely distributable under the terms of the MIT license.