diff --git a/README.md b/README.md index 6ec52b04..7613025f 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,6 @@ Table of Contents * [resolve()](#resolve) * [reject()](#reject) * [all()](#all) - * [race()](#race) * [any()](#any) * [some()](#some) * [map()](#map) @@ -387,8 +386,8 @@ $promise->then(function ($value) { Useful functions for creating, joining, mapping and reducing collections of promises. -All functions working on promise collections (like `all()`, `race()`, `some()` -etc.) support cancellation. This means, if you call `cancel()` on the returned +All functions working on promise collections (like `all()`, `some()` etc.) +support cancellation. This means, if you call `cancel()` on the returned promise, all promises in the collection are cancelled. #### resolve() @@ -436,15 +435,6 @@ Returns a promise that will resolve only once all the items in will be an array containing the resolution values of each of the items in `$promisesOrValues`. -#### race() - -```php -$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. - #### any() ```php diff --git a/src/functions.php b/src/functions.php index 8eb82220..38b00b40 100644 --- a/src/functions.php +++ b/src/functions.php @@ -41,24 +41,6 @@ function all(array $promisesOrValues) }); } -function race(array $promisesOrValues) -{ - if (!$promisesOrValues) { - return resolve(); - } - - $cancellationQueue = new Internal\CancellationQueue(); - - return new Promise(function ($resolve, $reject) use ($promisesOrValues, $cancellationQueue) { - foreach ($promisesOrValues as $promiseOrValue) { - $cancellationQueue->enqueue($promiseOrValue); - - resolve($promiseOrValue) - ->done($resolve, $reject); - } - }, $cancellationQueue); -} - function any(array $promisesOrValues) { return some($promisesOrValues, 1) diff --git a/tests/FunctionRaceTest.php b/tests/FunctionRaceTest.php deleted file mode 100644 index d8b531c6..00000000 --- a/tests/FunctionRaceTest.php +++ /dev/null @@ -1,156 +0,0 @@ -createCallableMock(); - $mock - ->expects($this->once()) - ->method('__invoke') - ->with($this->identicalTo(null)); - - race( - [] - )->then($mock); - } - - /** @test */ - public function shouldResolveValuesArray() - { - $mock = $this->createCallableMock(); - $mock - ->expects($this->once()) - ->method('__invoke') - ->with($this->identicalTo(1)); - - race( - [1, 2, 3] - )->then($mock); - } - - /** @test */ - public function shouldResolvePromisesArray() - { - $mock = $this->createCallableMock(); - $mock - ->expects($this->once()) - ->method('__invoke') - ->with($this->identicalTo(2)); - - $d1 = new Deferred(); - $d2 = new Deferred(); - $d3 = new Deferred(); - - race( - [$d1->promise(), $d2->promise(), $d3->promise()] - )->then($mock); - - $d2->resolve(2); - - $d1->resolve(1); - $d3->resolve(3); - } - - /** @test */ - public function shouldResolveSparseArrayInput() - { - $mock = $this->createCallableMock(); - $mock - ->expects($this->once()) - ->method('__invoke') - ->with($this->identicalTo(null)); - - race( - [null, 1, null, 2, 3] - )->then($mock); - } - - /** @test */ - public function shouldRejectIfFirstSettledPromiseRejects() - { - $mock = $this->createCallableMock(); - $mock - ->expects($this->once()) - ->method('__invoke') - ->with($this->identicalTo(2)); - - $d1 = new Deferred(); - $d2 = new Deferred(); - $d3 = new Deferred(); - - race( - [$d1->promise(), $d2->promise(), $d3->promise()] - )->then($this->expectCallableNever(), $mock); - - $d2->reject(2); - - $d1->resolve(1); - $d3->resolve(3); - } - - /** @test */ - public function shouldCancelInputArrayPromises() - { - $mock1 = $this - ->getMockBuilder('React\Promise\PromiseInterface') - ->getMock(); - $mock1 - ->expects($this->once()) - ->method('cancel'); - - $mock2 = $this - ->getMockBuilder('React\Promise\PromiseInterface') - ->getMock(); - $mock2 - ->expects($this->once()) - ->method('cancel'); - - race([$mock1, $mock2])->cancel(); - } - - /** @test */ - public function shouldNotCancelOtherPendingInputArrayPromisesIfOnePromiseFulfills() - { - $mock = $this->createCallableMock(); - $mock - ->expects($this->never()) - ->method('__invoke'); - - $deferred = New Deferred($mock); - $deferred->resolve(); - - $mock2 = $this - ->getMockBuilder('React\Promise\PromiseInterface') - ->getMock(); - $mock2 - ->expects($this->never()) - ->method('cancel'); - - race([$deferred->promise(), $mock2])->cancel(); - } - - /** @test */ - public function shouldNotCancelOtherPendingInputArrayPromisesIfOnePromiseRejects() - { - $mock = $this->createCallableMock(); - $mock - ->expects($this->never()) - ->method('__invoke'); - - $deferred = New Deferred($mock); - $deferred->reject(); - - $mock2 = $this - ->getMockBuilder('React\Promise\PromiseInterface') - ->getMock(); - $mock2 - ->expects($this->never()) - ->method('cancel'); - - race([$deferred->promise(), $mock2])->cancel(); - } -}