File tree Expand file tree Collapse file tree 2 files changed +34
-1
lines changed Expand file tree Collapse file tree 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> {
6969 resolve ( false ) ;
7070 }
7171 } , timeout ) ;
72- Promise . all ( this . _buffer )
72+ SyncPromise . all ( this . _buffer )
7373 . then ( ( ) => {
7474 clearTimeout ( capturedSetTimeout ) ;
7575 resolve ( true ) ;
Original file line number Diff line number Diff line change @@ -53,6 +53,39 @@ export class SyncPromise<T> implements Promise<T> {
5353 } ) ;
5454 }
5555
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+
5689 /** JSDoc */
5790 public then < TResult1 = T , TResult2 = never > (
5891 onfulfilled ?: ( ( value : T ) => TResult1 | PromiseLike < TResult1 > ) | null ,
You can’t perform that action at this time.
0 commit comments