Skip to content

Commit 9219299

Browse files
committed
Support iterable type for all() + race() + any()
1 parent 93d4b0f commit 9219299

File tree

5 files changed

+77
-11
lines changed

5 files changed

+77
-11
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ reject a promise, any language error or user land exception can be used to rejec
376376
#### all()
377377

378378
```php
379-
$promise = React\Promise\all(array $promisesOrValues);
379+
$promise = React\Promise\all(iterable $promisesOrValues);
380380
```
381381

382382
Returns a promise that will resolve only once all the items in
@@ -387,7 +387,7 @@ will be an array containing the resolution values of each of the items in
387387
#### race()
388388

389389
```php
390-
$promise = React\Promise\race(array $promisesOrValues);
390+
$promise = React\Promise\race(iterable $promisesOrValues);
391391
```
392392

393393
Initiates a competitive race that allows one winner. Returns a promise which is
@@ -399,7 +399,7 @@ contains 0 items.
399399
#### any()
400400

401401
```php
402-
$promise = React\Promise\any(array $promisesOrValues);
402+
$promise = React\Promise\any(iterable $promisesOrValues);
403403
```
404404

405405
Returns a promise that will resolve when any one of the items in

src/functions.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,15 @@ function reject(\Throwable $reason): PromiseInterface
6868
* will be an array containing the resolution values of each of the items in
6969
* `$promisesOrValues`.
7070
*
71-
* @param array $promisesOrValues
71+
* @param iterable $promisesOrValues
7272
* @return PromiseInterface
7373
*/
74-
function all(array $promisesOrValues): PromiseInterface
74+
function all(iterable $promisesOrValues): PromiseInterface
7575
{
76+
if (!\is_array($promisesOrValues)) {
77+
$promisesOrValues = \iterator_to_array($promisesOrValues);
78+
}
79+
7680
if (!$promisesOrValues) {
7781
return resolve([]);
7882
}
@@ -109,11 +113,15 @@ function ($mapped) use ($i, &$values, &$toResolve, $resolve): void {
109113
* The returned promise will become **infinitely pending** if `$promisesOrValues`
110114
* contains 0 items.
111115
*
112-
* @param array $promisesOrValues
116+
* @param iterable $promisesOrValues
113117
* @return PromiseInterface
114118
*/
115-
function race(array $promisesOrValues): PromiseInterface
119+
function race(iterable $promisesOrValues): PromiseInterface
116120
{
121+
if (!\is_array($promisesOrValues)) {
122+
$promisesOrValues = \iterator_to_array($promisesOrValues);
123+
}
124+
117125
if (!$promisesOrValues) {
118126
return new Promise(function (): void {});
119127
}
@@ -141,18 +149,22 @@ function race(array $promisesOrValues): PromiseInterface
141149
* The returned promise will also reject with a `React\Promise\Exception\LengthException`
142150
* if `$promisesOrValues` contains 0 items.
143151
*
144-
* @param array $promisesOrValues
152+
* @param iterable $promisesOrValues
145153
* @return PromiseInterface
146154
*/
147-
function any(array $promisesOrValues): PromiseInterface
155+
function any(iterable $promisesOrValues): PromiseInterface
148156
{
157+
if (!\is_array($promisesOrValues)) {
158+
$promisesOrValues = \iterator_to_array($promisesOrValues);
159+
}
160+
149161
$len = \count($promisesOrValues);
150162

151163
if (!$promisesOrValues) {
152164
return reject(
153165
new Exception\LengthException(
154166
\sprintf(
155-
'Input array must contain at least 1 item but contains only %s item%s.',
167+
'Must contain at least 1 item but contains only %s item%s.',
156168
$len,
157169
1 === $len ? '' : 's'
158170
)

tests/FunctionAllTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,24 @@ public function shouldResolveSparseArrayInput()
5858
->then($mock);
5959
}
6060

61+
/** @test */
62+
public function shouldResolveValuesGenerator()
63+
{
64+
$mock = $this->createCallableMock();
65+
$mock
66+
->expects(self::once())
67+
->method('__invoke')
68+
->with(self::identicalTo([1, 2, 3]));
69+
70+
$gen = (function () {
71+
for ($i = 1; $i <= 3; ++$i) {
72+
yield $i;
73+
}
74+
})();
75+
76+
all($gen)->then($mock);
77+
}
78+
6179
/** @test */
6280
public function shouldRejectIfAnyInputPromiseRejects()
6381
{

tests/FunctionAnyTest.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function shouldRejectWithLengthExceptionWithEmptyInputArray()
1818
->with(
1919
self::callback(function ($exception) {
2020
return $exception instanceof LengthException &&
21-
'Input array must contain at least 1 item but contains only 0 items.' === $exception->getMessage();
21+
'Must contain at least 1 item but contains only 0 items.' === $exception->getMessage();
2222
})
2323
);
2424

@@ -52,6 +52,24 @@ public function shouldResolveWithAPromisedInputValue()
5252
->then($mock);
5353
}
5454

55+
/** @test */
56+
public function shouldResolveValuesGenerator()
57+
{
58+
$mock = $this->createCallableMock();
59+
$mock
60+
->expects(self::once())
61+
->method('__invoke')
62+
->with(self::identicalTo(1));
63+
64+
$gen = (function () {
65+
for ($i = 1; $i <= 3; ++$i) {
66+
yield $i;
67+
}
68+
})();
69+
70+
any($gen)->then($mock);
71+
}
72+
5573
/** @test */
5674
public function shouldRejectWithAllRejectedInputValuesIfAllInputsAreRejected()
5775
{

tests/FunctionRaceTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,24 @@ public function shouldResolveSparseArrayInput()
6565
)->then($mock);
6666
}
6767

68+
/** @test */
69+
public function shouldResolveValuesGenerator()
70+
{
71+
$mock = $this->createCallableMock();
72+
$mock
73+
->expects(self::once())
74+
->method('__invoke')
75+
->with(self::identicalTo(1));
76+
77+
$gen = (function () {
78+
for ($i = 1; $i <= 3; ++$i) {
79+
yield $i;
80+
}
81+
})();
82+
83+
race($gen)->then($mock);
84+
}
85+
6886
/** @test */
6987
public function shouldRejectIfFirstSettledPromiseRejects()
7088
{

0 commit comments

Comments
 (0)