Skip to content

Commit

Permalink
Immediately resolve each promises that are passed an empty iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
jeskew committed Oct 15, 2015
1 parent 97fe721 commit 4cdb3fb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/EachPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ private function createPromise()
{
$this->aggregate = new Promise(function () {
reset($this->pending);
if (empty($this->pending) && !$this->iterable->valid()) {
$this->aggregate->resolve(null);
return;
}

// Consume a potentially fluctuating list of promises while
// ensuring that indexes are maintained (precluding array_shift).
while ($promise = current($this->pending)) {
Expand Down
6 changes: 6 additions & 0 deletions tests/EachPromiseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ public function testCanBeCancelled()
$this->markTestIncomplete();
}

public function testFulfillsImmediatelyWhenGivenAnEmptyIterator()
{
$each = new EachPromise(new \ArrayIterator([]));
$result = $each->promise()->wait();
}

public function testDoesNotBlowStackWithFulfilledPromises()
{
$pending = [];
Expand Down

0 comments on commit 4cdb3fb

Please sign in to comment.