javascript
26 lines · 7 steps
Bounded-concurrency async map in JavaScript
Run a worker over many items with a fixed number of tasks in flight, preserving result order.
Explained by
highlit
1async function mapWithConcurrency(items, limit, worker) {
2 const results = new Array(items.length);
3 let nextIndex = 0;
4
5 async function runner() {
6 while (true) {
7 const current = nextIndex++;
8 if (current >= items.length) return;
9 results[current] = await worker(items[current], current);
10 }
11 }
12
13 const pool = Array.from({ length: Math.min(limit, items.length) }, runner);
14 await Promise.all(pool);
15 return results;
16}
17
18async function mapSettledWithConcurrency(items, limit, worker) {
19 return mapWithConcurrency(items, limit, async (item, index) => {
20 try {
21 return { status: 'fulfilled', value: await worker(item, index) };
22 } catch (reason) {
23 return { status: 'rejected', reason };
24 }
25 });
26}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1A shared mutable index lets a fixed set of workers pull tasks without overlapping.
- 2Writing results by original index keeps output order independent of completion order.
- 3Wrapping each worker in try/catch turns a fail-fast map into an allSettled-style one.
Related explainers
javascript
const RETRIABLE_STATUS = new Set([408, 429, 500, 502, 503, 504]); function sleep(ms, signal) { return new Promise((resolve, reject) => {
Retrying fetch with exponential backoff
retry
exponential-backoff
abort-signal
Advanced
8 steps
java
public final class Debouncer<T> { private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(runnable -> {
How a debouncer collapses bursts in Java
debounce
concurrency
scheduling
Intermediate
9 steps
javascript
const express = require('express'); const Stripe = require('stripe'); const stripe = new Stripe(process.env.STRIPE_SECRET_KEY);
Verifying Stripe webhooks in Express
webhooks
signature-verification
raw-body
Intermediate
7 steps
ruby
class FundsTransfer class InsufficientFundsError < StandardError; end def initialize(source:, destination:, amount:)
Atomic money transfers with Rails transactions
service object
database transactions
row locking
Advanced
9 steps
javascript
import { cookies } from 'next/headers'; import { NextResponse } from 'next/server'; import { SignJWT, jwtVerify } from 'jose';
JWT session cookies in a Next.js Route Handler
authentication
jwt
cookies
Intermediate
8 steps
rust
use std::collections::HashMap; use std::sync::{Arc, Mutex}; use std::thread;
Aggregating metrics across threads in Rust
concurrency
shared-state
mutex
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/bounded-concurrency-async-map-in-javascript-explained-javascript-4e10/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.