iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🐈

[JavaScript] Synchronizing Async Processing in forEach Using a Semaphore

に公開
2022/07/20

Previous Article

In the previous article, we made sequential processing possible by chaining then of Promises. This time, I would like to implement a semaphore so that the maximum number of concurrent executions can be managed. However, since there are already libraries available that provide semaphore-like behavior, to differentiate this implementation, I'll try to implement a semaphore with the world's smallest amount of code.

https://zenn.dev/sora_kumo/articles/612ca66c68ff52

Implementation

So, here it is.

Since I've focused on reducing the amount of code and included functionality similar to Promise.all for waiting for completion, the source code is structured like a Rube Goldberg machine.

Parallel processing in JavaScript is not multi-threaded, so what this actually does is stack functions in an array for scheduling control and execute them according to the maximum concurrency limit.

const semaphore = (
  limit = 1,
  count = 0,
  rs = new Array<() => void>(),
  all?: () => void
) => ({
  acquire: () =>
    ++count > limit && new Promise<void>((resolve) => rs.push(resolve)),
  release: () => (--count ? rs.shift()?.() : all?.()),
  all: () => count && new Promise<void>((resolve) => (all = resolve)),
});

Common Asynchronous Function for Testing

It waits for 1 second after displaying the execution time and the content of the argument.

const f = (value: string) =>
  new Promise<void>((resolve) => {
    console.timeLog("debug", value);
    setTimeout(resolve, 1000);
  });

Source for Concurrency of 1 and Its Execution Results

const main = async () => {
  console.time("debug");
  const s = semaphore();
  ["A", "B", "C", "D", "E"].forEach(async (v) => {
    await s.acquire();
    await f(v);
    s.release();
  });
  await s.all(); // Wait for all to finish
  console.timeLog("debug", "Finished");
};
main();

You can see that it is being executed at approximately one-second intervals.
If the concurrency is 1, it becomes equivalent to sequential processing.

debug: 0.197ms A
debug: 1.014s B
debug: 2.027s C
debug: 3.039s D
debug: 4.040s E
debug: 5.050s Finished

Source for Concurrency of 2 and Its Execution Results

const main = async () => {
  console.time("debug");
  const s = semaphore(2);
  ["A", "B", "C", "D", "E"].forEach(async (v) => {
    await s.acquire();
    await f(v);
    s.release();
  });
  await s.all(); // Wait for all to finish
  console.timeLog("debug", "Finished");
};
main();

The value for B might look large, but please note that the unit is ms.
You can see that A and B execute almost at 0 seconds, C and D at 1 second, E at 2 seconds, and it finishes after 3 seconds.

debug: 0.19ms A
debug: 1.826ms B
debug: 1.005s C
debug: 1.005s D
debug: 2.012s E
debug: 3.028s Finished

Summary

As mentioned in the previous article, forEach itself is not an asynchronous process. This does not mean that async/await cannot be used. If you implement logic for synchronization, it will work as expected. If you intend to, it is possible to write it quickly without requiring a large amount of code.

GitHubで編集を提案

Discussion