@satankebab/promise-all-limited
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

Promise All Limited

Promise.all with parallel limit.

Install

yarn add install @satankebab/promise-all-limited

Example usage with fetch

import { promiseAllLimited } from '@satankebab/promise-all-limited';

const urlsToFetch = ['https://google.com', 'https://screenmanager.tech', 'https://mtgstocks.com', 'https://something-else.com'];

// Every fetch function needs to be wrapped with a function so we can control when the fetch is executed
const jobs = urlsToFetch.map(url => () => fetch(url))

promiseAllLimited(
  2,
  jobs,
).then(console.log)
// Will fetch those urls, maximum of parallel requests is 2

Error handling

It behaves the same as Promise.all. The only difference is that when an error occurs, the jobs that have not already started will not be started.

promiseAllLimited(
  2,
  jobs,
).then(console.log).catch(erorr => {
  console.error(error)
})

or

try {
  const result = await promiseAllLimited(
    2,
    jobs,
  )
  console.log(result)
} catch(error) {
  console.error(error)
}

or any way you prefer.

/@satankebab/promise-all-limited/

    Package Sidebar

    Install

    npm i @satankebab/promise-all-limited

    Weekly Downloads

    2

    Version

    1.0.0

    License

    ISC

    Unpacked Size

    29.4 kB

    Total Files

    11

    Last publish

    Collaborators

    • satankebab