export async function runWorkQueue(initial, concurrency = 10) { const pending = [...initial]; const active = new Set(); let head = 0; const enqueue = (task) => pending.push(task); try { while (head < pending.length || active.size) { while (head < pending.length && active.size < concurrency) { const task = pending[head]; pending[head++] = undefined; const running = Promise.resolve().then(() => task(enqueue)); active.add(running); running.then(() => active.delete(running), () => {}); } if (active.size) await Promise.race(active); } } catch (error) { await Promise.allSettled(active); throw error; } }