Skip to content

MartinPavlik/promise-all-limited

Repository files navigation

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.

About

Promise.all with parallel limit.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published