Skip to content

Commit 06a1d05

Browse files
committed
fix: throttle promise all infinite loop
@W-13829750@ remove generator in favor of shifting entries from queue
1 parent d978c9a commit 06a1d05

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

src/throttledPromiseAll.ts

+1-9
Original file line numberDiff line numberDiff line change
@@ -133,22 +133,14 @@ export class ThrottledPromiseAll<T, O = T> {
133133
}
134134

135135
private async dequeue(): Promise<void> {
136-
const generator = function* (
137-
data: Array<PromiseItem<T, O | undefined>>
138-
): Generator<PromiseItem<T, O | undefined> | undefined> {
139-
while (data.length > 0) {
140-
yield data.shift();
141-
}
142-
};
143136
const concurrencyPool: Map<number, Promise<IndexedResult<O> | undefined>> = new Map<
144137
number,
145138
Promise<IndexedResult<O> | undefined>
146139
>();
147-
const get = generator(this.queue);
148140
let index = 0;
149141
while (this.queue.length > 0 || concurrencyPool.size > 0) {
150142
while (concurrencyPool.size < this.concurrency) {
151-
const item = get.next().value as PromiseItem<T, O | undefined>;
143+
const item = this.queue.shift();
152144
if (!item) {
153145
break;
154146
}

test/promiseQueue.test.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66
*/
77
import { expect } from 'chai';
8-
import { ThrottledPromiseAll } from '../src';
9-
import { Duration } from '../src';
8+
import { Duration, ThrottledPromiseAll } from '../src';
109

1110
describe('throttledPromiseAll', () => {
1211
const numberProducer = (
@@ -80,10 +79,17 @@ describe('throttledPromiseAll', () => {
8079
throttledPromiseAll.add(
8180
1,
8281
(source: number, throttledPromise: ThrottledPromiseAll<number, number | undefined>): Promise<number> => {
82+
// add more to the queue after a couple of seconds
8383
if (source === 1) {
84-
throttledPromise.add([2, 3, 4, 5], numberProducer);
84+
return new Promise((resolve) => {
85+
setTimeout(() => {
86+
throttledPromise.add([2, 3, 4, 5], numberProducer);
87+
resolve(source + 1);
88+
}, 2000);
89+
});
90+
} else {
91+
return Promise.resolve(source + 1);
8592
}
86-
return Promise.resolve(source + 1);
8793
}
8894
);
8995
await throttledPromiseAll.all();

0 commit comments

Comments
 (0)