File tree Expand file tree Collapse file tree 1 file changed +13
-29
lines changed Expand file tree Collapse file tree 1 file changed +13
-29
lines changed Original file line number Diff line number Diff line change 1
- export async function runWithConcurrencyLimit ( tasks , limit ) {
2
- const running = new Set ( ) ;
3
-
4
- async function runNext ( ) {
5
- if ( tasks . length === 0 ) {
6
- return ;
7
- }
8
-
9
- const task = tasks . shift ( ) ;
10
- const promise = task ( )
11
- . catch ( error => {
12
- throw error ;
13
- } )
14
- . finally ( ( ) => {
15
- running . delete ( promise ) ;
16
- void runNext ( ) ;
17
- } ) ;
1
+ import { getLogger } from '@sitespeed.io/log' ;
2
+ const log = getLogger ( 'sitespeedio.plugin.s3' ) ;
18
3
19
- running . add ( promise ) ;
20
- if ( running . size < limit ) {
21
- void runNext ( ) ;
4
+ export async function runWithConcurrencyLimit ( tasks , limit ) {
5
+ async function worker ( ) {
6
+ while ( tasks . length > 0 ) {
7
+ const task = tasks . shift ( ) ;
8
+ try {
9
+ await task ( ) ;
10
+ } catch ( error ) {
11
+ log . error ( 'Could not finish upload task' , error ) ;
12
+ }
22
13
}
23
14
}
24
15
25
- const starters = [ ] ;
26
- for ( let index = 0 ; index < limit && tasks . length > 0 ; index ++ ) {
27
- starters . push ( runNext ( ) ) ;
28
- }
29
-
30
- await Promise . allSettled ( starters ) ;
31
- if ( running . size > 0 ) {
32
- await Promise . allSettled ( Array . from ( running ) ) ;
33
- }
16
+ const workers = Array . from ( { length : limit } , ( ) => worker ( ) ) ;
17
+ await Promise . all ( workers ) ;
34
18
}
You can’t perform that action at this time.
0 commit comments