Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propose limit.with API to streamline map() usage #74

Open
aaronccasanova opened this issue Nov 25, 2023 · 1 comment
Open

Propose limit.with API to streamline map() usage #74

aaronccasanova opened this issue Nov 25, 2023 · 1 comment

Comments

@aaronccasanova
Copy link

aaronccasanova commented Nov 25, 2023

Hello, I'd like to propose introducing a limit.with API to streamline the process of creating limited functions with Array.prototype.map()

Current API:

const limit = pLimit(5)

// With inline function
await Promise.all(
  items.map(item => limit(async () => {
    await process(item)
  }))
)

// With function declaration
async function processItem(item) {
  await process(item)
}

await Promise.all(
  items.map(item => limit(() => processItem(item)))
)

Proposed API:

const limit = pLimit(5)

// With inline function
await Promise.all(
  items.map(limit.with(async item => {
    await process(item)
  }))
)

// With function declaration
async function processItem(item) {
  await process(item)
}

await Promise.all(
  items.map(limit.with(processItem))
)

While the difference is subtle, the intention is to remove some boilerplate and improve readability. I put together a POC here and am happy to follow up with documentation, any API/naming/etc changes, and opening a PR

@septatrix
Copy link

While it does not allow you to reuse the limiter across several places you could also find p-map interesting: https://github.com/sindresorhus/p-map

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants