Skip to content

Commit d45bc6f

Browse files
authored
Merge pull request #83 from reactphp/fix-race
Fix race() to return a forever pending promise when called with an empty array
2 parents 2dfc2e8 + 8de97bc commit d45bc6f

4 files changed

Lines changed: 9 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ CHANGELOG for 3.x
1212
`some()`, `map()`, `reduce()`) now require an array of promises or values
1313
as input. Before, arrays and promises which resolve to an array were
1414
supported, other input types resolved to empty arrays or `null` (#35).
15+
* BC break: `race()` now returns a forever pending promise when called with
16+
an empty array (#83).
17+
The behavior is now also in line with the ES6 specification.
1518
* BC break: The interfaces `PromiseInterface`, `ExtendedPromiseInterface`
1619
and `CancellablePromiseInterface` have been merged into a single
1720
`PromiseInterface` (#75).

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,9 @@ $promise = React\Promise\race(array $promisesOrValues);
445445
Initiates a competitive race that allows one winner. Returns a promise which is
446446
resolved in the same way the first settled promise resolves.
447447

448+
The returned promise will become **infinitely pending** if `$promisesOrValues`
449+
contains 0 items.
450+
448451
#### any()
449452

450453
```php

src/functions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function all(array $promisesOrValues)
4444
function race(array $promisesOrValues)
4545
{
4646
if (!$promisesOrValues) {
47-
return resolve();
47+
return new Promise(function() {});
4848
}
4949

5050
$cancellationQueue = new Internal\CancellationQueue();

tests/FunctionRaceTest.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,11 @@
55
class FunctionRaceTest extends TestCase
66
{
77
/** @test */
8-
public function shouldResolveEmptyInput()
8+
public function shouldReturnForeverPendingPromiseForEmptyInput()
99
{
10-
$mock = $this->createCallableMock();
11-
$mock
12-
->expects($this->once())
13-
->method('__invoke')
14-
->with($this->identicalTo(null));
15-
1610
race(
1711
[]
18-
)->then($mock);
12+
)->then($this->expectCallableNever(), $this->expectCallableNever());
1913
}
2014

2115
/** @test */

0 commit comments

Comments
 (0)