Skip to content

Commit 9b2c528

Browse files
committed
Remove some() function
1 parent 4bc784e commit 9b2c528

File tree

4 files changed

+11
-246
lines changed

4 files changed

+11
-246
lines changed

README.md

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ Table of Contents
4242
* [all()](#all)
4343
* [race()](#race)
4444
* [any()](#any)
45-
* [some()](#some)
4645
* [map()](#map)
4746
* [reduce()](#reduce)
4847
4. [Examples](#examples)
@@ -368,7 +367,7 @@ once all consumers called the `cancel()` method of the promise.
368367
Useful functions for creating, joining, mapping and reducing collections of
369368
promises.
370369

371-
All functions working on promise collections (like `all()`, `race()`, `some()`
370+
All functions working on promise collections (like `all()`, `race()`,
372371
etc.) support cancellation. This means, if you call `cancel()` on the returned
373372
promise, all promises in the collection are cancelled.
374373

@@ -442,26 +441,6 @@ which holds all rejection reasons. The rejection reasons can be obtained with
442441
The returned promise will also reject with a `React\Promise\Exception\LengthException`
443442
if `$promisesOrValues` contains 0 items.
444443

445-
#### some()
446-
447-
```php
448-
$promise = React\Promise\some(array $promisesOrValues, integer $howMany);
449-
```
450-
451-
Returns a promise that will resolve when at least `$howMany` of the supplied items in
452-
`$promisesOrValues` fulfill. The resolution value of the returned promise
453-
will be an array of length `$howMany` containing the resolution values of
454-
`$howMany` fulfilled promises that were resolved first.
455-
456-
The returned promise will reject if it becomes impossible for `$howMany` items
457-
to resolve (that is, when `(count($promisesOrValues) - $howMany) + 1` items
458-
reject). The rejection value will be a `React\Promise\Exception\CompositeException`
459-
which holds `(count($promisesOrValues) - $howMany) + 1` rejection reasons.
460-
The rejection reasons can be obtained with `CompositeException::getExceptions()`.
461-
462-
The returned promise will also reject with a `React\Promise\Exception\LengthException`
463-
if `$promisesOrValues` contains less items than `$howMany`.
464-
465444
#### map()
466445

467446
```php

src/functions.php

Lines changed: 8 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -122,45 +122,13 @@ function race(array $promisesOrValues): PromiseInterface
122122
*/
123123
function 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
}

tests/FunctionAnyTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function shouldRejectWithAllRejectedInputValuesIfAllInputsAreRejected()
6161

6262
$compositeException = new CompositeException(
6363
[0 => $exception1, 1 => $exception2, 2 => $exception3],
64-
'Too many promises rejected.'
64+
'All promises rejected.'
6565
);
6666

6767
$mock = $this->createCallableMock();
@@ -126,6 +126,6 @@ public function shouldNotCancelOtherPendingInputArrayPromisesIfOnePromiseFulfill
126126

127127
$promise2 = new Promise(function () {}, $this->expectCallableNever());
128128

129-
some([$deferred->promise(), $promise2], 1)->cancel();
129+
any([$deferred->promise(), $promise2], 1)->cancel();
130130
}
131131
}

tests/FunctionSomeTest.php

Lines changed: 0 additions & 168 deletions
This file was deleted.

0 commit comments

Comments
 (0)