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-work-queue

v1.1.0

Published

A simple promise-based work queue to pipeline tasks

Downloads

41

Readme

promise-work-queue

Dependency Status npm version

Build a flexible promise-based workflow queue and then pipe payloads into it for concurrency-managed processing.

The point is to eliminate repeating the same function steps over and over - define them once, and send all your data into the queue as it arrives and have it processed by a set number of concurrent workers.

Installation

NPM

  • Latest release:

    $ npm install promise-work-queue

Example

const PromiseWorkQueue = require('promise-work-queue').default;

let queue = new PromiseWorkQueue(2); // new queue with 2 workers (default is 1)

queue.addStep(payload => ++payload); // incrememnt the payload
queue.addStep(payload => new Promise((resolve, reject) =>
  setTimeout(() => resolve(payload), 5000))); // delay it about 5 seconds
queue.addStep(payload => console.log(payload)); // print it out

queue.onDrain(() => console.log('Queue is empty'));

queue.addPayload(11);
queue.addPayload(42);
queue.addPayload(99);

// Will result in printing 12 and 43 in 5 seconds, and then 100 in about 10 seconds
// Finally, 'Queue is empty' will print

If you're using TypeScript, typings are included and you can do:

import PromiseWorkQueue from 'promise-work-queue';

// any type required here to allow last function to not have a return value
// or for cases where return values may mutate along the chain
const queue = new PromiseWorkQueue<any>(2); // new queue with 2 workers (default is 1)

queue.addStep((payload) => {
	return payload + 1;
}); // incrememnt the payload
queue.addStep((payload) => new Promise((resolve, reject) => setTimeout(() => resolve(payload), 5000))); // delay it about 5 seconds
queue.addStep((payload) => console.log(payload)); // print it out

queue.onDrain(() => console.log('Queue is empty'));

queue.addPayload(11);
queue.addPayload(42);
queue.addPayload(99);

//////

const numberQueue = new PromiseWorkQueue<number>(2); // new queue with 2 workers (default is 1)

numberQueue.addStep((payload) => {
	return payload + 1;
}); // incrememnt the payload
numberQueue.addStep((payload) => new Promise((resolve, reject) => setTimeout(() => resolve(payload), 5000))); // delay it about 5 seconds
numberQueue.addStep((payload) => {
	console.log(payload);
	return payload;
}); // print it out

numberQueue.onDrain(() => console.log('numberQueue is empty'));

numberQueue.addPayload(11);
numberQueue.addPayload(42);
numberQueue.addPayload(99);

You can get more complicated with passing the queue to another class and having it add its own steps, then adding your own post-processing later, etc