Skip to content

Commit 22a1dcf

Browse files
committed
chore: replace some concurrent promise requests with sequential
1 parent 396cbb3 commit 22a1dcf

3 files changed

Lines changed: 44 additions & 20 deletions

File tree

test/helpers.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ export const initClient = (options: Partial<CreateHttpClientParams> = {}) => {
4646
}
4747
return createClient({
4848
accessToken,
49-
throttle: 3,
49+
throttle: 5,
5050
...params,
5151
...options,
5252
})
5353
}
5454

5555
// Shared instance to reduce rate limiting issues due to recreation of clients and therefore loosing track of requests per second
56-
export const defaultClient = initClient({ throttle: 3, ...params })
56+
export const defaultClient = initClient({ throttle: 5, ...params })
5757

5858
/**
5959
* @returns {import('../lib/contentful-management').PlainClientAPI}
@@ -62,7 +62,7 @@ export const initPlainClient = (defaults = {}) => {
6262
return createClient(
6363
{
6464
accessToken,
65-
throttle: 3,
65+
throttle: 5,
6666
...params,
6767
},
6868
{
@@ -190,7 +190,7 @@ export const cleanupTaxonomy = async (olderThan = 1000 * 60 * 60) => {
190190
console.log(`Deleting ${conceptsToBeDeleted.length} concepts`)
191191
}
192192

193-
await Promise.all(
193+
await promiseAllSequential(
194194
conceptsToBeDeleted.map((item) =>
195195
client.concept.delete({
196196
conceptId: item.sys.id,
@@ -252,3 +252,19 @@ export async function waitForBulkActionV2Processing<T extends BulkActionV2Payloa
252252
options,
253253
)
254254
}
255+
256+
/**
257+
* Executes an array of promises sequentially, waiting for each one to complete
258+
* before starting the next one. This is useful for rate limiting or when you need
259+
* to avoid overwhelming an API with concurrent requests.
260+
*
261+
* @param promises Array of promises to execute sequentially
262+
* @returns Promise that resolves to an array of results in the same order
263+
*/
264+
export async function promiseAllSequential<T>(promises: Promise<T>[]): Promise<T[]> {
265+
const results: T[] = []
266+
for (const promise of promises) {
267+
results.push(await promise)
268+
}
269+
return results
270+
}

test/integration/tag-integration.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { describe, it, beforeAll, afterAll, afterEach } from 'vitest'
1+
import { describe, it, beforeAll, afterAll, afterEach, beforeEach } from 'vitest'
22
import { expect } from 'vitest'
33
import {
44
defaultClient,
55
generateRandomId,
66
createTestSpace,
77
timeoutToCalmRateLimiting,
8+
promiseAllSequential,
89
} from '../helpers'
910
import type { Space, Environment, Tag, Link } from '../../lib/export-types'
1011

@@ -29,8 +30,7 @@ describe('Tags API', () => {
2930

3031
afterEach(async () => {
3132
const tags = await environment.getTags({ limit: 1000 })
32-
const deleting = tags.items.map((tag) => tag.delete())
33-
await Promise.allSettled(deleting)
33+
await promiseAllSequential(tags.items.map((tag) => tag.delete()))
3434
})
3535

3636
afterAll(async () => {

test/integration/taxonomy-integration.test.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { describe, it, beforeEach, afterEach, expect, afterAll } from 'vitest'
22
import type { ConceptProps, CreateConceptProps } from '../../lib/entities/concept'
33
import type { ConceptSchemeProps, CreateConceptSchemeProps } from '../../lib/export-types'
4-
import { getTestOrganizationId, initPlainClient, timeoutToCalmRateLimiting } from '../helpers'
4+
import {
5+
getTestOrganizationId,
6+
initPlainClient,
7+
promiseAllSequential,
8+
timeoutToCalmRateLimiting,
9+
} from '../helpers'
510

611
let conceptsToDelete: ConceptProps[] = []
712
let conceptSchemesToDelete: ConceptSchemeProps[] = []
@@ -214,7 +219,7 @@ describe('Taxonomy Integration', () => {
214219
})
215220

216221
it('concept getTotal', async () => {
217-
await Promise.all(
222+
await promiseAllSequential(
218223
Array(3)
219224
.fill(null)
220225
.map(async (i) => {
@@ -235,7 +240,7 @@ describe('Taxonomy Integration', () => {
235240
})
236241

237242
it('gets a list of all concepts', async () => {
238-
await Promise.all(
243+
await promiseAllSequential(
239244
Array(3)
240245
.fill(null)
241246
.map(async (i) => {
@@ -255,7 +260,7 @@ describe('Taxonomy Integration', () => {
255260
})
256261

257262
it('gets a list of all paginated concepts', async () => {
258-
await Promise.all(
263+
await promiseAllSequential(
259264
Array(3)
260265
.fill(null)
261266
.map(async (i) => {
@@ -526,7 +531,7 @@ describe('Taxonomy Integration', () => {
526531
})
527532

528533
it('conceptScheme getTotal', async () => {
529-
await Promise.all(
534+
const results = await promiseAllSequential(
530535
Array(3)
531536
.fill(null)
532537
.map(async (i) => {
@@ -536,18 +541,19 @@ describe('Taxonomy Integration', () => {
536541
},
537542
}
538543

539-
const result = await client.conceptScheme.create({}, conceptScheme)
540-
conceptSchemesToDelete.push(result)
544+
return client.conceptScheme.create({}, conceptScheme)
541545
}),
542546
)
543547

548+
conceptSchemesToDelete.push(...results)
549+
544550
const { total } = await client.conceptScheme.getTotal({})
545551

546552
expect(total).toBe(3)
547553
})
548554

549555
it('gets a list of all concept schemes', async () => {
550-
await Promise.all(
556+
const results = await promiseAllSequential(
551557
Array(3)
552558
.fill(null)
553559
.map(async (i) => {
@@ -557,17 +563,18 @@ describe('Taxonomy Integration', () => {
557563
},
558564
}
559565

560-
const result = await client.conceptScheme.create({}, conceptScheme)
561-
conceptSchemesToDelete.push(result)
566+
return client.conceptScheme.create({}, conceptScheme)
562567
}),
563568
)
564569

570+
conceptSchemesToDelete.push(...results)
571+
565572
const { items } = await client.conceptScheme.getMany({})
566573
expect(items.length).toBe(3)
567574
})
568575

569576
it('gets a list of all paginated concept schemes', async () => {
570-
await Promise.all(
577+
const results = await promiseAllSequential(
571578
Array(3)
572579
.fill(null)
573580
.map(async (i) => {
@@ -577,11 +584,12 @@ describe('Taxonomy Integration', () => {
577584
},
578585
}
579586

580-
const result = await client.conceptScheme.create({}, conceptScheme)
581-
conceptSchemesToDelete.push(result)
587+
return client.conceptScheme.create({}, conceptScheme)
582588
}),
583589
)
584590

591+
conceptSchemesToDelete.push(...results)
592+
585593
const { items, pages } = await client.conceptScheme.getMany({
586594
query: {
587595
limit: 2,

0 commit comments

Comments
 (0)