Ну если не нравится класс то замена функция
function sleep(time = 1000) {
return new Promise((resolve) => {
setTimeout(resolve, time);
});
}
function createThread(run: (isRunning: () => boolean, sleep?: (time: number) => Promise<unknown>) => Promise<unknown>) {
let running = false
const stop = () => {
running = false
}
const start = () => {
if (running) return;
running = true
run(() => running, sleep)
}
return { start, stop }
}