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

tiny-promise-pool

v1.3.1

Published

Tiny library for performing promises while limiting concurrency

Downloads

67

Readme

Tiny Promise Pool

A tiny library to execute multiple promises in parallel, keeping not to execute more than N promises at any one time, using a promise.

  • Tiny Promise Pool is reviewed by Codacy Badge
  • Tiny promise Pool is unit tested with

Install:

npm i tiny-promise-pool

Use with creator function:

var promisePool = require('tiny-promise-pool')

function nextPromise({index, data}) {
  if (index>=20) return null // no more
  return new Promise(function(res, rej) {
    res(index*2 + data)
  })
}

var all = promisePool({
  threads: 3,                 // maximum parallel promises
  promises: nextPromise,  // function to get/generate next promise
  context_data: 17       // user data for the next_promise function
})

all.then(function(result) {
  console.dir(result)         // after all promises are resolved
})

Result:

[
  { context: { index: 0, thread: 0, data: 17, ended: 10 }, promise: Promise { 17 }, result: 17 },
  { context: { index: 1, thread: 1, data: 17, ended: 1 }, promise: Promise { 19 }, result: 19 },
  { context: { index: 2, thread: 2, data: 17, ended: 7 }, promise: Promise { 21 }, result: 21 },
  { context: { index: 3, thread: 0, data: 17, ended: 6 }, promise: Promise { 23 }, result: 23 },
  { context: { index: 4, thread: 1, data: 17, ended: 2 }, promise: Promise { 25 }, result: 25 },
  { context: { index: 5, thread: 2, data: 17, ended: 3 }, promise: Promise { 27 }, result: 27 },
  { context: { index: 6, thread: 0, data: 17, ended: 4 }, promise: Promise { 29 }, result: 29 },
  { context: { index: 7, thread: 1, data: 17, ended: 5 }, promise: Promise { 31 }, result: 31 },
  ...
  { context: { index: 19, thread: 1, data: 17, ended: 19 }, promise: Promise { 55 }, result: 55 }
]

Use with array of promises:

function makePromise(i) {
  return new Promise(function (resolve, _reject) {
    resolve(i)
  })
}

const promiseList = [
  makePromise(0),
  makePromise(1)
]

promisePool({
  threads: 3,
  promises: promiseList,              // List of promises
  context_data: 'data for context'
}).then(function(result) {
  ...
})

Using generator to generate promises:

function *createPromiseMaker() {
  for (var i=0; i<10; i+=1) {
    yield new Promise(...)
  }
}

const pool = promisePool({
  threads: 3,
  promises: createPromiseMaker()
})

Nested promise pools:

const innerPool = promisePool({
  threads: 2,
  promises: [
    makePromise(2),
    makePromise(3),
    makePromise(4),
    makePromise(5)
  ],
  context_data: 'secondary promise pool'
})
const pool = promisePool({
  threads: 3,
  promises: [
    makePromise(0),
    makePromise(1),
    innerPool
  ],
  context_data: 'primary promise pool'
})
pool.then(function(result) {
  ...
}

Debugging tool:

promisePool = require('tiny-promise-pool')
visualize = require('tiny-promise-pool/promise-pool-visualize')
const pool = promisePool({
  ...
})
pool.then(visualize).then(result => {
   ...
})

TODO list:

  • API similar to Promise.all(): "Promise.pool([promise, ...], threads)"
  • options for behavior on reject:
    • reject as soon as one is rejected (same as Promise.all)
    • wait for all to resolve even if some are rejected (same as Promise.when)
  • support listeners for individual promise completions/rejections

migration from previous versions:

the following option keys are renamed:

  1. next_promise_data -> context_data
  2. next_promise -> promises
  3. max_parallel -> threads

you can still use the older option keys, but they are deprecated