Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

promisedip

thomasjuster13MIT1.1.6TypeScript support: included

Promise pooler to manage concurrency

promise, pool, pooler, promise pool, promise pooling, promise pooler, promise concurrency, promise-pool, promise-pooling, promise-pooler, promise-concurrency, concurrency, typed, typescript, browser, node, isomorphic, module, JavaScript module, EcmaScript module, ESNext

readme

pipeline status coverage report npm version Downloads js-standard-style Commitizen friendly

NOTE: This lib exposes next-gen code ; this means that you might need polyfills in old environments (e.g.: node < 7.6 or IE).

It ships with:

Installation

NPM

npm i -S promisedip
yarn add promisedip

JavaScript module

<script type="module">
  import PromisePool from 'https://unpkg.com/promisedip@1.0.2/lib/PromisePool.esm.js';
  // …
</script>

Demo

I made a codesandox to visualize the promise pooling (in the console)

API

NB: Everything is TypeScript-friendly

new PromisePool(options)

Usage:

new PromisePool(options: { concurrency: number })

Example

import PromisePool from 'promisedip'

const pool = new PromisePool({ concurrency: 2 })

async function doStuff (): Promise<void> {
  const [
    examplePage,
    wordscapePage,
    wiki,
  ] = await Promise.all([
    // pool will execute the following promises in the correct order respecting the concurrency option provided
    pool.resolve(() => fetch('https://example.com/')),
    pool.resolve(() => fetch('https://wordscape.com/')),
    pool.resolve(async () => {
      const response = await fetch('https://en.wikipedia.org/')
      // … do anything else you need
      return response
    }),
    // and so on…
  ])
}

PromisePool.map()

Usage

PromisePool.map<T, U>(
  array: Array<T>,
  mapper: (item: T, index: number, array: Array<T>) => Promise<U>,
  options: { concurrency: number },
): Promise<U[]>

Example

import PromisePool from 'promisedip'

const pages = [
  'https://example.com/',
  'https://wordscape.com/',
  'https://en.wikipedia.org/',
]

async function main (): Promise<void> {
  const results: string[] = await PromisePool.map(
    pages,
    async (page) => {
      const response = await fetch(page)
      return response.text()
    },
    { concurrency: 2 },
  )
}

Troubleshooting

  • PromisePool.map(readOnlyArray, …) does not work with a readonly array
    • Use PromisePool.map([...readOnlyArray], …)
  • The lib does not seem to work with Node, or TypeScript, or anywhere except the browser
    • TL;DR: install a version >= 1.1.4
    • Explanation: I made successive mistakes in the build process, I didn't master fully exports map nor rollupjs for building UMD, CommonJS AND ESModule outputs. Now everything is fine.

Why

This package is clearly not the only one to perform promise pooling. So why "yet another …" ?

Because …

  • I wanted a dependency-free package that I could use for frontend projects
  • Since I intend to use it on frontend apps, I wanted it to be extremely lightweight: the JavaScript module build is less than 1kB
  • I wanted a pooler that I could use at different scopes, from very global to very local
  • I wanted to be able to add promises in the stack on the fly
  • I wanted a proper naming
  • I wanted proper typings
  • I wanted JavaScript modules

changelog

1.1.5 (2020-10-28)

  • 1.1.5 (d3eadfd)
  • docs(changelog): generate (ce01c66)
  • docs(readme): add troubleshooting section (794fc77)

1.1.4 (2020-10-28)

  • 1.1.4 (dd06926)
  • build(package.json): fix export map, my bad… (f93af51)
  • docs(changelog): generate (ffee67f)

1.1.3 (2020-10-27)

  • 1.1.3 (da729d2)
  • chore(package): fill the new 'exports' package.json field (8f48ba4)
  • chore(package): fix issues url (2ff12fd)
  • docs(changelog): generate (427a7ef)
  • docs(readme): add a demo in codesandbox (c027426)

1.1.2 (2020-10-27)

  • 1.1.2 (b0a1fce)
  • docs(changelog): generate (78af144)
  • docs(readme): change absurd statement :shrug: (9abb46f)
  • docs(readme): remove parenthesis (ffc416e)

1.1.1 (2020-10-27)

  • 1.1.1 (7b1bae2)
  • docs(changelog): generate (325b60c)
  • docs(readme): suggest another example (fa18f48)
  • docs(readme): update examples (39411c5)

1.1.0 (2020-10-27)

  • 1.1.0 (0c4f753)
  • build(babel): remove babel since it was breaking the build (ffe814f)
  • docs(changelog): last fix to generate properly the changelog (ec99c6c)

1.0.4 (2020-10-26)

  • 1.0.4 (ba77384)
  • docs(changelog): update changelog (a1a42db)
  • docs(readme): add a word on typescript (50549a5)
  • docs(readme): be more precise, you can never be more precise (1db77c3)
  • docs(readme): change badges order for readability (5d71847)

1.0.3 (2020-10-26)

  • 1.0.3 (dfe61b2)
  • docs(readme): add badges (44a05ab)
  • test(map method): add test to obtain 100% coverage (cf54926)
  • ci(coverage): enable code coverage (2da4686)
  • ci(gitlab): set up ci (94f53a5)
  • style(lint): setup eslint properly and fix source files (b63862d)
  • chore(changelog): use commitizen as a commit & changelog tool (5dc3184)

1.0.2 (2020-10-26)

  • 1.0.2 (3a3a1db)
  • remove comment (0b8efcc)
  • chore(build): enhance rollup config and minification (dffdeee)

1.0.1 (2020-10-23)