From de1a946d52595264bc2a0f84675ddac97344e906 Mon Sep 17 00:00:00 2001 From: Jan Sorgalla Date: Wed, 11 Jan 2017 09:18:39 +0100 Subject: [PATCH 1/2] Fix race() to return a forever pending promise when called with an empty array --- CHANGELOG.md | 3 +++ src/functions.php | 2 +- tests/FunctionRaceTest.php | 10 ++-------- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b02ac528..11424f30 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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). diff --git a/src/functions.php b/src/functions.php index 8eb82220..40f6639c 100644 --- a/src/functions.php +++ b/src/functions.php @@ -44,7 +44,7 @@ function all(array $promisesOrValues) function race(array $promisesOrValues) { if (!$promisesOrValues) { - return resolve(); + return new Promise(function() {}); } $cancellationQueue = new Internal\CancellationQueue(); diff --git a/tests/FunctionRaceTest.php b/tests/FunctionRaceTest.php index d8b531c6..bae9c409 100644 --- a/tests/FunctionRaceTest.php +++ b/tests/FunctionRaceTest.php @@ -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 */ From 8de97bce8691b779c9abd447ac99a2ee71a00bc6 Mon Sep 17 00:00:00 2001 From: Jan Sorgalla Date: Fri, 13 Jan 2017 20:44:38 +0100 Subject: [PATCH 2/2] Add note about forever pending promise return by race() --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 6ec52b04..2490c83b 100644 --- a/README.md +++ b/README.md @@ -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