From 6e758d86a11be1e6380bf2a5c8bc29e5ea4e3d87 Mon Sep 17 00:00:00 2001 From: Devin Ivy Date: Sun, 12 Apr 2026 22:55:49 -0400 Subject: [PATCH] refactor: use setTimeout from node:timers/promises Co-Authored-By: Claude Opus 4.6 (1M context) --- src/worker-pool.ts | 3 ++- test/async-dispose.test.ts | 3 ++- test/fixtures/async-dispose.ts | 5 +++-- test/shared/mutex.test.ts | 3 ++- test/shared/rwlock.test.ts | 7 ++++--- 5 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/worker-pool.ts b/src/worker-pool.ts index 3e42243..3e77fc8 100644 --- a/src/worker-pool.ts +++ b/src/worker-pool.ts @@ -1,3 +1,4 @@ +import { setTimeout } from 'node:timers/promises'; import { Worker } from 'node:worker_threads'; import { availableParallelism } from 'node:os'; import { setupWorker, execute, dispatchStream } from './execute.ts'; @@ -79,7 +80,7 @@ export function workers(size: number = availableParallelism(), opts?: WorkerOpti if (opts?.shutdownTimeout != null) { await Promise.race([ settle, - new Promise((r) => setTimeout(r, opts.shutdownTimeout)), + setTimeout(opts.shutdownTimeout), ]); } else { await settle; diff --git a/test/async-dispose.test.ts b/test/async-dispose.test.ts index 97e7c9c..f8373c9 100644 --- a/test/async-dispose.test.ts +++ b/test/async-dispose.test.ts @@ -1,3 +1,4 @@ +import { setTimeout } from 'node:timers/promises'; import { describe, it } from 'node:test'; import assert from 'node:assert/strict'; import { workers } from 'moroutine'; @@ -51,7 +52,7 @@ describe('async dispose', () => { } })(); // Let a couple items flow before disposing - await new Promise((r) => setTimeout(r, 60)); + await setTimeout(60); await run[Symbol.asyncDispose](); await iterating; assert.equal(results.length, 5); diff --git a/test/fixtures/async-dispose.ts b/test/fixtures/async-dispose.ts index 725dd91..718b154 100644 --- a/test/fixtures/async-dispose.ts +++ b/test/fixtures/async-dispose.ts @@ -1,7 +1,8 @@ +import { setTimeout } from 'node:timers/promises'; import { mo } from 'moroutine'; export const slowTask = mo(import.meta, async (ms: number): Promise => { - await new Promise((r) => setTimeout(r, ms)); + await setTimeout(ms); return 'done'; }); @@ -14,7 +15,7 @@ export const waitForAbort = mo(import.meta, (signal: AbortSignal): Promise setTimeout(r, 20)); + await setTimeout(20); yield i; } }); diff --git a/test/shared/mutex.test.ts b/test/shared/mutex.test.ts index 5dbcbe4..8f02eab 100644 --- a/test/shared/mutex.test.ts +++ b/test/shared/mutex.test.ts @@ -1,3 +1,4 @@ +import { setTimeout } from 'node:timers/promises'; import { describe, it } from 'node:test'; import assert from 'node:assert/strict'; import { mutex } from 'moroutine'; @@ -32,7 +33,7 @@ describe('Mutex', () => { const task = async (id: number) => { const guard = await m.lock(); order.push(id); - await new Promise((r) => setTimeout(r, 10)); + await setTimeout(10); order.push(id); m.unlock(); }; diff --git a/test/shared/rwlock.test.ts b/test/shared/rwlock.test.ts index b368d4f..0ba3615 100644 --- a/test/shared/rwlock.test.ts +++ b/test/shared/rwlock.test.ts @@ -1,3 +1,4 @@ +import { setTimeout } from 'node:timers/promises'; import { describe, it } from 'node:test'; import assert from 'node:assert/strict'; import { rwlock } from 'moroutine'; @@ -39,7 +40,7 @@ describe('RwLock', () => { rw.readUnlock(); })(); - await new Promise((r) => setTimeout(r, 20)); + await setTimeout(20); order.push('write-end'); rw.writeUnlock(); @@ -60,7 +61,7 @@ describe('RwLock', () => { rw.writeUnlock(); })(); - await new Promise((r) => setTimeout(r, 20)); + await setTimeout(20); order.push('w1-end'); rw.writeUnlock(); @@ -111,7 +112,7 @@ describe('RwLock', () => { })(); // Let readers queue up - await new Promise((r) => setTimeout(r, 20)); + await setTimeout(20); rw.writeUnlock(); const results = await Promise.all([reader1Done, reader2Done]); -- 2.51.2