@@ -37,19 +37,35 @@ function all($promisesOrValues)
3737
3838function race ($ promisesOrValues )
3939{
40- return resolve ($ promisesOrValues )
41- ->then (function ($ array ) {
42- if (!is_array ($ array ) || !$ array ) {
43- return resolve ();
44- }
40+ $ cancellationQueue = new CancellationQueue ();
41+ $ cancellationQueue ->enqueue ($ promisesOrValues );
42+
43+ return new Promise (function ($ resolve , $ reject , $ notify ) use ($ promisesOrValues , $ cancellationQueue ) {
44+ resolve ($ promisesOrValues )
45+ ->done (function ($ array ) use ($ cancellationQueue , $ resolve , $ reject , $ notify ) {
46+ if (!is_array ($ array ) || !$ array ) {
47+ $ resolve ();
48+ return ;
49+ }
4550
46- return new Promise (function ($ resolve , $ reject , $ notify ) use ($ array ) {
4751 foreach ($ array as $ promiseOrValue ) {
52+ $ cancellationQueue ->enqueue ($ promiseOrValue );
53+
54+ $ fulfiller = function ($ value ) use ($ cancellationQueue , $ resolve ) {
55+ $ cancellationQueue ();
56+ $ resolve ($ value );
57+ };
58+
59+ $ rejecter = function ($ reason ) use ($ cancellationQueue , $ reject ) {
60+ $ cancellationQueue ();
61+ $ reject ($ reason );
62+ };
63+
4864 resolve ($ promiseOrValue )
49- ->done ($ resolve , $ reject , $ notify );
65+ ->done ($ fulfiller , $ rejecter , $ notify );
5066 }
51- });
52- } );
67+ }, $ reject , $ notify );
68+ }, $ cancellationQueue );
5369}
5470
5571function any ($ promisesOrValues )
@@ -62,28 +78,33 @@ function any($promisesOrValues)
6278
6379function some ($ promisesOrValues , $ howMany )
6480{
65- return resolve ($ promisesOrValues )
66- ->then (function ($ array ) use ($ howMany ) {
67- if (!is_array ($ array ) || !$ array || $ howMany < 1 ) {
68- return resolve ([]);
69- }
81+ $ cancellationQueue = new CancellationQueue ();
82+ $ cancellationQueue ->enqueue ($ promisesOrValues );
83+
84+ return new Promise (function ($ resolve , $ reject , $ notify ) use ($ promisesOrValues , $ howMany , $ cancellationQueue ) {
85+ resolve ($ promisesOrValues )
86+ ->done (function ($ array ) use ($ howMany , $ cancellationQueue , $ resolve , $ reject , $ notify ) {
87+ if (!is_array ($ array ) || !$ array || $ howMany < 1 ) {
88+ $ resolve ([]);
89+ return ;
90+ }
7091
71- return new Promise (function ($ resolve , $ reject , $ notify ) use ($ array , $ howMany ) {
7292 $ len = count ($ array );
7393 $ toResolve = min ($ howMany , $ len );
7494 $ toReject = ($ len - $ toResolve ) + 1 ;
7595 $ values = [];
7696 $ reasons = [];
7797
7898 foreach ($ array as $ i => $ promiseOrValue ) {
79- $ fulfiller = function ($ val ) use ($ i , &$ values , &$ toResolve , $ toReject , $ resolve ) {
99+ $ fulfiller = function ($ val ) use ($ i , &$ values , &$ toResolve , $ toReject , $ resolve, $ cancellationQueue ) {
80100 if ($ toResolve < 1 || $ toReject < 1 ) {
81101 return ;
82102 }
83103
84104 $ values [$ i ] = $ val ;
85105
86106 if (0 === --$ toResolve ) {
107+ $ cancellationQueue ();
87108 $ resolve ($ values );
88109 }
89110 };
@@ -100,26 +121,34 @@ function some($promisesOrValues, $howMany)
100121 }
101122 };
102123
124+ $ cancellationQueue ->enqueue ($ promiseOrValue );
125+
103126 resolve ($ promiseOrValue )
104127 ->done ($ fulfiller , $ rejecter , $ notify );
105128 }
106- });
107- } );
129+ }, $ reject , $ notify );
130+ }, $ cancellationQueue );
108131}
109132
110133function map ($ promisesOrValues , callable $ mapFunc )
111134{
112- return resolve ($ promisesOrValues )
113- ->then (function ($ array ) use ($ mapFunc ) {
114- if (!is_array ($ array ) || !$ array ) {
115- return resolve ([]);
116- }
135+ $ cancellationQueue = new CancellationQueue ();
136+ $ cancellationQueue ->enqueue ($ promisesOrValues );
137+
138+ return new Promise (function ($ resolve , $ reject , $ notify ) use ($ promisesOrValues , $ mapFunc , $ cancellationQueue ) {
139+ resolve ($ promisesOrValues )
140+ ->done (function ($ array ) use ($ mapFunc , $ cancellationQueue , $ resolve , $ reject , $ notify ) {
141+ if (!is_array ($ array ) || !$ array ) {
142+ $ resolve ([]);
143+ return ;
144+ }
117145
118- return new Promise (function ($ resolve , $ reject , $ notify ) use ($ array , $ mapFunc ) {
119146 $ toResolve = count ($ array );
120147 $ values = [];
121148
122149 foreach ($ array as $ i => $ promiseOrValue ) {
150+ $ cancellationQueue ->enqueue ($ promiseOrValue );
151+
123152 resolve ($ promiseOrValue )
124153 ->then ($ mapFunc )
125154 ->done (
@@ -134,35 +163,45 @@ function ($mapped) use ($i, &$values, &$toResolve, $resolve) {
134163 $ notify
135164 );
136165 }
137- });
138- } );
166+ }, $ reject , $ notify );
167+ }, $ cancellationQueue );
139168}
140169
141170function reduce ($ promisesOrValues , callable $ reduceFunc , $ initialValue = null )
142171{
143- return resolve ($ promisesOrValues )
144- ->then (function ($ array ) use ($ reduceFunc , $ initialValue ) {
145- if (!is_array ($ array )) {
146- $ array = [];
147- }
148-
149- $ total = count ($ array );
150- $ i = 0 ;
151-
152- // Wrap the supplied $reduceFunc with one that handles promises and then
153- // delegates to the supplied.
154- $ wrappedReduceFunc = function ($ current , $ val ) use ($ reduceFunc , $ total , &$ i ) {
155- return resolve ($ current )
156- ->then (function ($ c ) use ($ reduceFunc , $ total , &$ i , $ val ) {
157- return resolve ($ val )
158- ->then (function ($ value ) use ($ reduceFunc , $ total , &$ i , $ c ) {
159- return $ reduceFunc ($ c , $ value , $ i ++, $ total );
160- });
161- });
162- };
163-
164- return array_reduce ($ array , $ wrappedReduceFunc , $ initialValue );
165- });
172+ $ cancellationQueue = new CancellationQueue ();
173+ $ cancellationQueue ->enqueue ($ promisesOrValues );
174+
175+ return new Promise (function ($ resolve , $ reject , $ notify ) use ($ promisesOrValues , $ reduceFunc , $ initialValue , $ cancellationQueue ) {
176+ resolve ($ promisesOrValues )
177+ ->done (function ($ array ) use ($ reduceFunc , $ initialValue , $ cancellationQueue , $ resolve , $ reject , $ notify ) {
178+ if (!is_array ($ array )) {
179+ $ array = [];
180+ }
181+
182+ $ total = count ($ array );
183+ $ i = 0 ;
184+
185+ // Wrap the supplied $reduceFunc with one that handles promises and then
186+ // delegates to the supplied.
187+ $ wrappedReduceFunc = function ($ current , $ val ) use ($ reduceFunc , $ cancellationQueue , $ total , &$ i ) {
188+ $ cancellationQueue ->enqueue ($ val );
189+
190+ return $ current
191+ ->then (function ($ c ) use ($ reduceFunc , $ total , &$ i , $ val ) {
192+ return resolve ($ val )
193+ ->then (function ($ value ) use ($ reduceFunc , $ total , &$ i , $ c ) {
194+ return $ reduceFunc ($ c , $ value , $ i ++, $ total );
195+ });
196+ });
197+ };
198+
199+ $ cancellationQueue ->enqueue ($ initialValue );
200+
201+ array_reduce ($ array , $ wrappedReduceFunc , resolve ($ initialValue ))
202+ ->done ($ resolve , $ reject , $ notify );
203+ }, $ reject , $ notify );
204+ }, $ cancellationQueue );
166205}
167206
168207// Internal functions
0 commit comments