🕵️ SicherheitslückenWhatsApp-Schwachstelle: Zugriff auf Fotos bei gesperrtem Android-Handy(03.09.2026 um 09:18 Uhr)
🎥 PodcastsAuslegungssache 167: Datenschutz mit System(04.09.2026 um 06:10 Uhr)
🕵️ SicherheitslückenJetzt patchen! Es laufen derzeit Schadcode-Attacken auf Chrome(04.09.2026 um 08:48 Uhr)
🕵️ SicherheitslückenRoot-Sicherheitslücke bedroht cPanel/WHM(31.08.2026 um 09:33 Uhr)
🕵️ SicherheitslückenWhatsApp-Schwachstelle: Zugriff auf Fotos bei gesperrtem Android-Handy(03.09.2026 um 09:18 Uhr)
🎥 PodcastsAuslegungssache 167: Datenschutz mit System(04.09.2026 um 06:10 Uhr)
🕵️ SicherheitslückenJetzt patchen! Es laufen derzeit Schadcode-Attacken auf Chrome(04.09.2026 um 08:48 Uhr)
🕵️ SicherheitslückenRoot-Sicherheitslücke bedroht cPanel/WHM(31.08.2026 um 09:33 Uhr)

🔧 Programmierung 🕛 kürzlich 3 Min Lesezeit
0

How to Control the Number of Concurrent Promises in JavaScript

↗ Quelle (dev.to)
🗣️ Stimme:

In some cases, we may need to control the number of concurrent requests.



For example, when we are writing download or crawling tools, some websites may have a limit on the number of concurrent requests.



In browsers, the maximum number of TCP connections from the same origin is limited to 6. This means that if you use HTTP1.1 when sending more than 6 requests at the same time, the 7th request will wait until the previous one is processed before it starts.



We can use the following simple example to test it. First the client code:




CODE
void (async () => {
await Promise.all(
[...new Array(12)].map((_, i) =>
fetch(`http://127.0.0.1:3001/get/${i + 1}`)
)
);
})();






Next is the brief code for the service:




CODE
router.get('/get/:id', async (ctx) => {
const order = Number(ctx.params.id);
if (order % 2 === 0 && order <= 6) {
await sleep(1000);
}
await sleep(1000);
ctx.body = 1;
});






In the first 6 requests, if the order is even, then wait for 2s , if not, wait for 1s. Then open the Network in DevTools and you can get the following picture:



on Github. Here’s the same Gist:




CODE
class Queue<T> {
#tasks: T[] = [];

enqueue = (task: T) => {
this.#tasks.push(task);
};

dequeue = () => this.#tasks.shift();

clear = () => {
this.#tasks = [];
};

get size() {
return this.#tasks.length;
}
}

class PromiseLimiter {
#queue = new Queue<() => Promise<void>>();
#runningCount = 0;
#limitCount: number;

constructor(limitCount: number) {
this.#limitCount = limitCount;
}

#next = () => {
if (this.#runningCount < this.#limitCount && this.#queue.size > 0) {
this.#queue.dequeue()?.();
}
};

#run = async <R = any>(
fn: () => Promise<R>,
resolve: (value: PromiseLike<R>) => void
) => {
this.#runningCount += 1;
const result = (async () => fn())();
resolve(result);

try {
await result;
} catch {
// ignore
}

this.#runningCount -= 1;
this.#next();
};

get activeCount() {
return this.#runningCount;
}

get pendingCount() {
return this.#queue.size;
}

limit = <R = any>(fn: () => Promise<R>) =>
new Promise<R>((resolve) => {
this.#queue.enqueue(() => this.#run(fn, resolve));
this.#next();
});
}

export default PromiseLimiter;






This can be achieved in less than 70 lines of code above, you can try it out in StackBlitz.



You can see that it works as expected. Without resorting to any third-party libraries, the simple code model is just that.



So do you have any other use cases?

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 53%
🟡 In Evaluierung 21%
🟢 Keine Auswirkung 14%
Spannende Innovation 12%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
2 Quellen
Jetzt patchen! Angreifer attackieren JFrog Artifactory und machen sich zu Admins
1 Quelle
WhatsApp-Schwachstelle: Zugriff auf Fotos bei gesperrtem Android-Handy
1 Quelle
OpenAI-Agenten streiten beim Hacken über Ethik – und machen trotzdem weiter
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten How to Control the Number of Concurrent Promises in JavaScript

Thematisch verwandte Begriffe: Control, Number, Concurrent, Promises · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...