Async Map with Concurrency Limit
Async Map with Concurrency Limit is a reusable JavaScript snippet. Maps an array using an async function, limiting the number of concurrent operations. Prevents overwhelming APIs or databases. The full implementation: async function asyncMapLimit(arr, limit, fn) { const results = [] const executing = new Set() for (const [index, item] of arr.entries()) { const promise = fn(item, index, arr).then(r => ({ index, value: r })) executing.add(promise) promise.finally(() => executing.delete(promise)) if (executing.size >= limit) { const completed = await Promise.race(executing) results[completed.index] = completed.value } } // Wait for remaining const remaining = await Promise.all(executing) for (const r of remaining) results[r.index] = r.value return results } // Usage const urls = ['url1', 'url2',… This snippet uses only standard JavaScript with no dependencies, so it runs in modern browsers and Node.js alike. Browse more patterns in the snippet library, or paste the code straight into the playground to experiment with it.