async_pool.ts

TypeScript•Master

Explorer

C40
C++40
C#40
Dart40
Go40
Java40
JavaScript40
Kotlin40
Objective-C40
Perl40
PHP40
Python40
R40
Ruby40
Rust40
Shell40
Swift40
TypeScript
Beginner10
Intermediate10
Advanced10
Expert5
Master5
dependency_inject.ts
deep_readonly.ts
reactive_proxy.ts
middleware_chain.ts
async_pool.ts
async_pool.ts
Click to focus
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class AsyncPool<T> {↵
   private queue: (() => Promise<T>)[] = [];↵
   constructor(private limit: number) {}↵
   async exec<R>(fn: () => Promise<R>): Promise<R> {↵
       while (this.queue.length >= this.limit) {↵
           await Promise.race(this.queue);↵
       }↵
       const promise = fn().finally(() =>↵
           this.queue.splice(this.queue.indexOf(promise), 1)↵
       );↵
       this.queue.push(promise);↵
       return promise;↵
   }↵
}
0WPM•0%•0:00•0/436(0%)|Ln 1, Col 1
UTF-8•TypeScript

Related Exercises