Skip to content

Commit

Permalink
Fix wait() foreign promise compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
joshdifabio committed Dec 14, 2016
1 parent b551b52 commit 4d6e1e7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/Promise.php
Expand Up @@ -263,10 +263,17 @@ private function invokeWaitList()
$this->waitList = null;

foreach ($waitList as $result) {
$result->waitIfPending();
while ($result->result instanceof Promise) {
$result = $result->result;
while (true) {
$result->waitIfPending();

if ($result->result instanceof Promise) {
$result = $result->result;
} else {
if ($result->result instanceof PromiseInterface) {
$result->result->wait(false);
}
break;
}
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions tests/CoroutineTest.php
Expand Up @@ -2,6 +2,7 @@
namespace GuzzleHttp\Promise\Tests;

use GuzzleHttp\Promise\Coroutine;
use GuzzleHttp\Promise\Promise;
use GuzzleHttp\Promise\PromiseInterface;
use PHPUnit_Framework_TestCase;
use ReflectionClass;
Expand Down Expand Up @@ -62,4 +63,19 @@ public function testShouldCancelResultPromiseAndOutsideCurrentPromise()

$coroutine->cancel();
}

public function testWaitShouldResolveChainedCoroutines()
{
$promisor = function () {
return \GuzzleHttp\Promise\coroutine(function () {
yield $promise = new Promise(function () use (&$promise) {
$promise->resolve(1);
});
});
};

$promise = $promisor()->then($promisor)->then($promisor);

$this->assertSame(1, $promise->wait());
}
}

0 comments on commit 4d6e1e7

Please sign in to comment.