UNPKG

@deftomat/opinionated

Version:

Opinionated tooling for JavaScript & TypeScript projects.

25 lines (24 loc) 566 B
import os from 'node:os'; let currentlyRunning = 0; const queue = []; /** * "Allocates" one CPU core. */ export async function allocateCore() { const free = () => { currentlyRunning--; const startNext = queue.shift(); if (startNext) startNext(); }; if (currentlyRunning < os.cpus().length) { currentlyRunning++; return Promise.resolve({ free }); } return new Promise(resolve => { queue.push(() => { currentlyRunning++; resolve({ free }); }); }); }