Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

932 B
Raw Blame
/**
 * Map items with a bounded number of in-flight async operations while
 * preserving input order in the returned array.
 */
export async function mapWithConcurrency(items, concurrency, fn) {
  if (!Array.isArray(items)) {
    throw new TypeError('mapWithConcurrency: items must be an array');
  }
  if (typeof fn !== 'function') {
    throw new TypeError('mapWithConcurrency: fn must be a function');
  }

  const requestedConcurrency = Number(concurrency);
  const normalizedConcurrency = Number.isFinite(requestedConcurrency)
    ? Math.max(1, Math.floor(requestedConcurrency))
    : 1;
  const workerCount = Math.min(items.length, normalizedConcurrency);
  const results = new Array(items.length);
  let cursor = 0;

  await Promise.all(Array.from({ length: workerCount }, async () => {
    while (cursor < items.length) {
      const i = cursor++;
      results[i] = await fn(items[i], i);
    }
  }));

  return results;
}