diff --git a/server/api/github/contributors-evolution/[owner]/[repo].get.ts b/server/api/github/contributors-evolution/[owner]/[repo].get.ts index 24499a53a7..47cde6df85 100644 --- a/server/api/github/contributors-evolution/[owner]/[repo].get.ts +++ b/server/api/github/contributors-evolution/[owner]/[repo].get.ts @@ -1,3 +1,4 @@ +import { setTimeout } from 'node:timers/promises' import { CACHE_MAX_AGE_ONE_DAY } from '#shared/utils/constants' type GitHubContributorWeek = { @@ -12,8 +13,6 @@ type GitHubContributorStats = { weeks: GitHubContributorWeek[] } -const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)) - export default defineCachedEventHandler( async event => { const owner = getRouterParam(event, 'owner') @@ -50,7 +49,7 @@ export default defineCachedEventHandler( if (status === 202) { if (attempt === maxAttempts - 1) return [] - await sleep(delayMs) + await setTimeout(delayMs) delayMs = Math.min(delayMs * 2, 16_000) continue } diff --git a/server/utils/atproto/lock.ts b/server/utils/atproto/lock.ts index 21f00dc2b2..b91a1ac812 100644 --- a/server/utils/atproto/lock.ts +++ b/server/utils/atproto/lock.ts @@ -1,3 +1,4 @@ +import { setTimeout } from 'node:timers/promises' import type { RuntimeLock } from '@atproto/oauth-client-node' import { requestLocalLock } from '@atproto/oauth-client-node' import { Redis } from '@upstash/redis' @@ -22,7 +23,7 @@ function createUpstashLock(redis: Redis): RuntimeLock { if (!acquired) { // Another instance holds the lock, wait briefly and retry once - await new Promise(resolve => setTimeout(resolve, 100)) + await setTimeout(100) const retryAcquired = await redis.set(lockKey, lockValue, { nx: true, ex: lockTTL, diff --git a/test/unit/server/utils/atproto/lock.spec.ts b/test/unit/server/utils/atproto/lock.spec.ts index adf018e855..0cf6abb037 100644 --- a/test/unit/server/utils/atproto/lock.spec.ts +++ b/test/unit/server/utils/atproto/lock.spec.ts @@ -1,3 +1,4 @@ +import { setTimeout } from 'node:timers/promises' import { describe, expect, it, vi, beforeEach } from 'vitest' const mockRedisSet = vi.fn() @@ -145,7 +146,7 @@ describe('lock', () => { const lock = getUpstashLock() const result = await lock('async-key', async () => { - await new Promise(resolve => setTimeout(resolve, 10)) + await setTimeout(10) return 'async-result' }) diff --git a/test/unit/shared/utils/async.spec.ts b/test/unit/shared/utils/async.spec.ts index 93dd1451fa..a0f7271cbe 100644 --- a/test/unit/shared/utils/async.spec.ts +++ b/test/unit/shared/utils/async.spec.ts @@ -1,3 +1,4 @@ +import { setTimeout } from 'node:timers/promises' import { describe, expect, it, vi } from 'vitest' import * as fc from 'fast-check' import { mapWithConcurrency } from '#shared/utils/async' @@ -21,7 +22,7 @@ describe('mapWithConcurrency', () => { async () => { concurrent++ maxConcurrent = Math.max(maxConcurrent, concurrent) - await new Promise(resolve => setTimeout(resolve, 10)) + await setTimeout(10) concurrent-- }, 3, @@ -66,7 +67,7 @@ describe('mapWithConcurrency', () => { await mapWithConcurrency(items, async () => { concurrent++ maxConcurrent = Math.max(maxConcurrent, concurrent) - await new Promise(resolve => setTimeout(resolve, 5)) + await setTimeout(5) concurrent-- }) @@ -84,7 +85,7 @@ describe('mapWithConcurrency', () => { async () => { concurrent++ maxConcurrent = Math.max(maxConcurrent, concurrent) - await new Promise(resolve => setTimeout(resolve, 10)) + await setTimeout(10) concurrent-- }, 10,