silverwolf-x/project / packages /next /src /client /components /promise-queue.test.ts
silverwolf-x's picture
download
raw
1.21 kB
import { PromiseQueue } from './promise-queue'
describe('PromiseQueue', () => {
it('should limit the number of concurrent promises', async () => {
const queue = new PromiseQueue(2)
const results: number[] = []
const promises = Array.from({ length: 5 }, (_, i) =>
queue.enqueue(async () => {
results.push(i)
await new Promise((resolve) => setTimeout(resolve, 100))
return i
})
)
const resolved = await Promise.all(promises)
expect(resolved).toEqual([0, 1, 2, 3, 4])
expect(results).toEqual([0, 1, 2, 3, 4])
})
it('should allow bumping a promise to be next in the queue', async () => {
const queue = new PromiseQueue(2)
const results: number[] = []
const promises = Array.from({ length: 5 }, (_, i) =>
queue.enqueue(async () => {
results.push(i)
await new Promise((resolve) => setTimeout(resolve, 100))
return i
})
)
queue.bump(promises[3])
const resolved = await Promise.all(promises)
// 3 was bumped to be next in the queue but did not cancel the other promises before it
expect(results).toEqual([0, 1, 3, 2, 4])
expect(resolved).toEqual([0, 1, 2, 3, 4])
})
})

Xet Storage Details

Size:
1.21 kB
·
Xet hash:
e9ac592cf83ac179a1226bb33054fc48e1e23efdfadbff27951e2f78e797092c

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.