Skip to content

Commit 2eee0b5

Browse files
authored
refactor: use timers/promises setTimeout for async delays (#2377)
1 parent 06d1eb0 commit 2eee0b5

File tree

4 files changed

+10
-8
lines changed

4 files changed

+10
-8
lines changed

server/api/github/contributors-evolution/[owner]/[repo].get.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { setTimeout } from 'node:timers/promises'
12
import { CACHE_MAX_AGE_ONE_DAY } from '#shared/utils/constants'
23

34
type GitHubContributorWeek = {
@@ -12,8 +13,6 @@ type GitHubContributorStats = {
1213
weeks: GitHubContributorWeek[]
1314
}
1415

15-
const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms))
16-
1716
export default defineCachedEventHandler(
1817
async event => {
1918
const owner = getRouterParam(event, 'owner')
@@ -50,7 +49,7 @@ export default defineCachedEventHandler(
5049

5150
if (status === 202) {
5251
if (attempt === maxAttempts - 1) return []
53-
await sleep(delayMs)
52+
await setTimeout(delayMs)
5453
delayMs = Math.min(delayMs * 2, 16_000)
5554
continue
5655
}

server/utils/atproto/lock.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { setTimeout } from 'node:timers/promises'
12
import type { RuntimeLock } from '@atproto/oauth-client-node'
23
import { requestLocalLock } from '@atproto/oauth-client-node'
34
import { Redis } from '@upstash/redis'
@@ -22,7 +23,7 @@ function createUpstashLock(redis: Redis): RuntimeLock {
2223

2324
if (!acquired) {
2425
// Another instance holds the lock, wait briefly and retry once
25-
await new Promise(resolve => setTimeout(resolve, 100))
26+
await setTimeout(100)
2627
const retryAcquired = await redis.set(lockKey, lockValue, {
2728
nx: true,
2829
ex: lockTTL,

test/unit/server/utils/atproto/lock.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { setTimeout } from 'node:timers/promises'
12
import { describe, expect, it, vi, beforeEach } from 'vitest'
23

34
const mockRedisSet = vi.fn()
@@ -145,7 +146,7 @@ describe('lock', () => {
145146

146147
const lock = getUpstashLock()
147148
const result = await lock('async-key', async () => {
148-
await new Promise(resolve => setTimeout(resolve, 10))
149+
await setTimeout(10)
149150
return 'async-result'
150151
})
151152

test/unit/shared/utils/async.spec.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { setTimeout } from 'node:timers/promises'
12
import { describe, expect, it, vi } from 'vitest'
23
import * as fc from 'fast-check'
34
import { mapWithConcurrency } from '#shared/utils/async'
@@ -21,7 +22,7 @@ describe('mapWithConcurrency', () => {
2122
async () => {
2223
concurrent++
2324
maxConcurrent = Math.max(maxConcurrent, concurrent)
24-
await new Promise(resolve => setTimeout(resolve, 10))
25+
await setTimeout(10)
2526
concurrent--
2627
},
2728
3,
@@ -66,7 +67,7 @@ describe('mapWithConcurrency', () => {
6667
await mapWithConcurrency(items, async () => {
6768
concurrent++
6869
maxConcurrent = Math.max(maxConcurrent, concurrent)
69-
await new Promise(resolve => setTimeout(resolve, 5))
70+
await setTimeout(5)
7071
concurrent--
7172
})
7273

@@ -84,7 +85,7 @@ describe('mapWithConcurrency', () => {
8485
async () => {
8586
concurrent++
8687
maxConcurrent = Math.max(maxConcurrent, concurrent)
87-
await new Promise(resolve => setTimeout(resolve, 10))
88+
await setTimeout(10)
8889
concurrent--
8990
},
9091
10,

0 commit comments

Comments
 (0)