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

simple-promise-queue

v0.1.7

Published

queue where you can push promise-like functions

Downloads

91

Readme

This is a simple queue where you can push a function(resolve, reject) to a queue and get a promise that references that task.

Install

npm install simple-promise-queue

Use Cases

  • Use case #1: Throttled Promise.all
  • Use case #2: Used as a task queuer to grab a resource with limited connection pools

How to use

Queue.pushTask(function(resolve, reject) { ... }) Puts a task at the end of the queue. Returns a Promise.

Queue.unshiftTask(function(resolve, reject) { ... }) Puts a task at the beginning of the queue. Returns a Promise.

simple-promise-queue inherits from queue, so check that out for other methods.

Note: I added an option autoStart that can be passed into the constructor so the queue will automatically start whenever something is pushed into it.

Examples

Adding your own Promise implementation:

var Queue = require('simple-promise-queue');

Queue.setPromise(require('bluebird'));

Pushing a task to the queue and getting a promise:

var Queue = require('simple-promise-queue');

var queue = new Queue({
  autoStart: true, // autostart the queue
});

var promise = queue.pushTask(function(resolve, reject) {
  // do some task here to fetch results
  resolve('results');
});

promise.then(function(results) {
  // process the results here
});

A useful example of this would be if you had Promise.all but wanted to only throttle it to run 5 tasks at a time:

var Queue = require('simple-promise-queue');

var queue = new Queue({
  autoStart: true, // autostart the queue
  concurrency: 5
});

var promiseArr = [];

var updateUserInDb = function(id) {
  var promise = queue.pushTask(function(resolve, reject) {
    // do a query to update the user here and
    resolve('done');
  });

  promiseArr.push(promise);
};

var promiseArr = [];
for (var id = 0; id < 100; id++) {
  queue.updateUserInDb(id);
}

Promise.all(promiseArr).then(function() {
  // will call this after all the updates have been run
});

Here is an example where you have 2 jobs (each having 2 tasks) to complete and want to know when each job is finished individually.

var Queue = require('simple-promise-queue');

var queue = new Queue({
  autoStart: true, // autostart the queue
  concurrency: 3
});

var job1Part1 = queue.pushTask(function(resolve, reject) {
  // some limited resource async task that takes 5 seconds
  setTimeout(function() {
    resolve('promise 1 done');
  }, 5000);
});

var job1Part2 = queue.pushTask(function(resolve, reject) {
  // some limited resource async task that takes 3 seconds
  setTimeout(function() {
    resolve('promise 2 done');
  }, 3000);
});

var job2Part1 = queue.pushTask(function(resolve, reject) {
  // some limited resource async task that takes 5 seconds
  setTimeout(function() {
    resolve('promise 1 done');
  }, 5000);
});

var job2Part2 = queue.pushTask(function(resolve, reject) {
  // some limited resource async task that takes 3 seconds
  setTimeout(function() {
    resolve('promise 2 done');
  }, 3000);
});

Promise.all([job1Part1, job1Part2]).then(function(values) {
  // this should take 5 seconds to reach here
});

Promise.all([job2Part1, job2Part2]).then(function(values) {
  // this should take 8 seconds to reach here
});

Testing

  1. Run the command inside this folder ./node_modules/mocha/bin/mocha