JavaScript Interview Question #195

How would you limit concurrency when processing many asynchronous operations?

Advanced JavaScript, Practical Problems & Senior Interview Scenarios Senior Advanced

Quick Interview Answer

Starting thousands of asynchronous operations at once can overload the browser, server, network, database, or external API. A concurrency limit processes only a controlled number of operations simultaneously.

Detailed Explanation

Starting thousands of asynchronous operations at once can overload the browser, server, network, database, or external API. A concurrency limit processes only a controlled number of operations simultaneously.

The exact implementation can use a worker pool, queue, semaphore-like abstraction, or a library. A senior answer should recognize that Promise.all provides concurrency but does not itself impose a maximum concurrency limit.

Code Example

async function runWithWorkers(
    items,
    workerCount,
    processItem
) {
    let index = 0;

    async function worker() {
        while (index < items.length) {
            const current = items[index++];
            await processItem(current);
        }
    }

    await Promise.all(
        Array.from(
            { length: workerCount },
            () => worker()
        )
    );
}

await runWithWorkers(
    products,
    4,
    saveProduct
);