Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix race() to return a forever pending promise when called with an empty array #83

Merged
merged 2 commits into from
Jan 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ CHANGELOG for 3.x
`some()`, `map()`, `reduce()`) now require an array of promises or values
as input. Before, arrays and promises which resolve to an array were
supported, other input types resolved to empty arrays or `null` (#35).
* BC break: `race()` now returns a forever pending promise when called with
an empty array (#83).
The behavior is now also in line with the ES6 specification.
* BC break: The interfaces `PromiseInterface`, `ExtendedPromiseInterface`
and `CancellablePromiseInterface` have been merged into a single
`PromiseInterface` (#75).
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,9 @@ $promise = React\Promise\race(array $promisesOrValues);
Initiates a competitive race that allows one winner. Returns a promise which is
resolved in the same way the first settled promise resolves.

The returned promise will become **infinitely pending** if `$promisesOrValues`
contains 0 items.

#### any()

```php
Expand Down
2 changes: 1 addition & 1 deletion src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function all(array $promisesOrValues)
function race(array $promisesOrValues)
{
if (!$promisesOrValues) {
return resolve();
return new Promise(function() {});
}

$cancellationQueue = new Internal\CancellationQueue();
Expand Down
10 changes: 2 additions & 8 deletions tests/FunctionRaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,11 @@
class FunctionRaceTest extends TestCase
{
/** @test */
public function shouldResolveEmptyInput()
public function shouldReturnForeverPendingPromiseForEmptyInput()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->identicalTo(null));

race(
[]
)->then($mock);
)->then($this->expectCallableNever(), $this->expectCallableNever());
}

/** @test */
Expand Down