File tree 2 files changed +34
-1
lines changed
2 files changed +34
-1
lines changed Original file line number Diff line number Diff line change @@ -69,7 +69,7 @@ export class PromiseBuffer<T> {
69
69
resolve ( false ) ;
70
70
}
71
71
} , timeout ) ;
72
- Promise . all ( this . _buffer )
72
+ SyncPromise . all ( this . _buffer )
73
73
. then ( ( ) => {
74
74
clearTimeout ( capturedSetTimeout ) ;
75
75
resolve ( true ) ;
Original file line number Diff line number Diff line change @@ -53,6 +53,39 @@ export class SyncPromise<T> implements Promise<T> {
53
53
} ) ;
54
54
}
55
55
56
+ /** JSDoc */
57
+ public static all < U = any > ( collection : Array < U | PromiseLike < U > > ) : Promise < U [ ] > {
58
+ return new SyncPromise < U [ ] > ( ( resolve , reject ) => {
59
+ if ( ! Array . isArray ( collection ) ) {
60
+ reject ( new TypeError ( `Promise.all requires an array as input.` ) ) ;
61
+ return ;
62
+ }
63
+
64
+ if ( collection . length === 0 ) {
65
+ resolve ( [ ] ) ;
66
+ return ;
67
+ }
68
+
69
+ let counter = collection . length ;
70
+ const resolvedCollection : U [ ] = [ ] ;
71
+
72
+ collection . forEach ( ( item , index ) => {
73
+ SyncPromise . resolve ( item )
74
+ . then ( value => {
75
+ resolvedCollection [ index ] = value ;
76
+ counter -= 1 ;
77
+
78
+ if ( counter !== 0 ) {
79
+ return ;
80
+ }
81
+
82
+ resolve ( resolvedCollection ) ;
83
+ } )
84
+ . catch ( reject ) ;
85
+ } ) ;
86
+ } ) ;
87
+ }
88
+
56
89
/** JSDoc */
57
90
public then < TResult1 = T , TResult2 = never > (
58
91
onfulfilled ?: ( ( value : T ) => TResult1 | PromiseLike < TResult1 > ) | null ,
You can’t perform that action at this time.
0 commit comments