@@ -122,45 +122,13 @@ function race(array $promisesOrValues): PromiseInterface
122122 */
123123function any (array $ promisesOrValues ): PromiseInterface
124124{
125- return some ($ promisesOrValues , 1 )
126- ->then (function ($ val ) {
127- return \array_shift ($ val );
128- });
129- }
130-
131- /**
132- * Returns a promise that will resolve when `$howMany` of the supplied items in
133- * `$promisesOrValues` resolve. The resolution value of the returned promise
134- * will be an array of length `$howMany` containing the resolution values of the
135- * triggering items.
136- *
137- * The returned promise will reject if it becomes impossible for `$howMany` items
138- * to resolve (that is, when `(count($promisesOrValues) - $howMany) + 1` items
139- * reject). The rejection value will be an array of
140- * `(count($promisesOrValues) - $howMany) + 1` rejection reasons.
141- *
142- * The returned promise will also reject with a `React\Promise\Exception\LengthException`
143- * if `$promisesOrValues` contains less items than `$howMany`.
144- *
145- * @param array $promisesOrValues
146- * @param int $howMany
147- * @return PromiseInterface
148- */
149- function some (array $ promisesOrValues , int $ howMany ): PromiseInterface
150- {
151- if ($ howMany < 1 ) {
152- return resolve ([]);
153- }
154-
155125 $ len = \count ($ promisesOrValues );
156126
157- if ($ len < $ howMany ) {
127+ if (! $ promisesOrValues ) {
158128 return reject (
159129 new Exception \LengthException (
160130 \sprintf (
161- 'Input array must contain at least %d item%s but contains only %s item%s. ' ,
162- $ howMany ,
163- 1 === $ howMany ? '' : 's ' ,
131+ 'Input array must contain at least 1 item but contains only %s item%s. ' ,
164132 $ len ,
165133 1 === $ len ? '' : 's '
166134 )
@@ -170,37 +138,23 @@ function some(array $promisesOrValues, int $howMany): PromiseInterface
170138
171139 $ cancellationQueue = new Internal \CancellationQueue ();
172140
173- return new Promise (function ($ resolve , $ reject ) use ($ len , $ promisesOrValues , $ howMany , $ cancellationQueue ): void {
174- $ toResolve = $ howMany ;
175- $ toReject = ($ len - $ toResolve ) + 1 ;
176- $ values = [];
141+ return new Promise (function ($ resolve , $ reject ) use ($ len , $ promisesOrValues , $ cancellationQueue ): void {
142+ $ toReject = $ len ;
177143 $ reasons = [];
178144
179145 foreach ($ promisesOrValues as $ i => $ promiseOrValue ) {
180- $ fulfiller = function ($ val ) use ($ i , &$ values , &$ toResolve , $ toReject , $ resolve ): void {
181- if ($ toResolve < 1 || $ toReject < 1 ) {
182- return ;
183- }
184-
185- $ values [$ i ] = $ val ;
186-
187- if (0 === --$ toResolve ) {
188- $ resolve ($ values );
189- }
146+ $ fulfiller = function ($ val ) use ($ resolve ): void {
147+ $ resolve ($ val );
190148 };
191149
192- $ rejecter = function (\Throwable $ reason ) use ($ i , &$ reasons , &$ toReject , $ toResolve , $ reject ): void {
193- if ($ toResolve < 1 || $ toReject < 1 ) {
194- return ;
195- }
196-
150+ $ rejecter = function (\Throwable $ reason ) use ($ i , &$ reasons , &$ toReject , $ reject ): void {
197151 $ reasons [$ i ] = $ reason ;
198152
199153 if (0 === --$ toReject ) {
200154 $ reject (
201155 new CompositeException (
202156 $ reasons ,
203- 'Too many promises rejected. '
157+ 'All promises rejected. '
204158 )
205159 );
206160 }
0 commit comments