Skip to content

Commit fdc0d6a

Browse files
Merge pull request #55 from joshdifabio/fix/promise-wait
Fix wait() foreign promise compatibility Closes #54 Closes guzzle/guzzle#1689
2 parents b551b52 + 23382a1 commit fdc0d6a

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

src/Promise.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,10 +263,17 @@ private function invokeWaitList()
263263
$this->waitList = null;
264264

265265
foreach ($waitList as $result) {
266-
$result->waitIfPending();
267-
while ($result->result instanceof Promise) {
268-
$result = $result->result;
266+
while (true) {
269267
$result->waitIfPending();
268+
269+
if ($result->result instanceof Promise) {
270+
$result = $result->result;
271+
} else {
272+
if ($result->result instanceof PromiseInterface) {
273+
$result->result->wait(false);
274+
}
275+
break;
276+
}
270277
}
271278
}
272279
}

tests/CoroutineTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace GuzzleHttp\Promise\Tests;
33

44
use GuzzleHttp\Promise\Coroutine;
5+
use GuzzleHttp\Promise\Promise;
56
use GuzzleHttp\Promise\PromiseInterface;
67
use PHPUnit_Framework_TestCase;
78
use ReflectionClass;
@@ -62,4 +63,43 @@ public function testShouldCancelResultPromiseAndOutsideCurrentPromise()
6263

6364
$coroutine->cancel();
6465
}
66+
67+
public function testWaitShouldResolveChainedCoroutines()
68+
{
69+
$promisor = function () {
70+
return \GuzzleHttp\Promise\coroutine(function () {
71+
yield $promise = new Promise(function () use (&$promise) {
72+
$promise->resolve(1);
73+
});
74+
});
75+
};
76+
77+
$promise = $promisor()->then($promisor)->then($promisor);
78+
79+
$this->assertSame(1, $promise->wait());
80+
}
81+
82+
public function testWaitShouldHandleIntermediateErrors()
83+
{
84+
$promise = \GuzzleHttp\Promise\coroutine(function () {
85+
yield $promise = new Promise(function () use (&$promise) {
86+
$promise->resolve(1);
87+
});
88+
})
89+
->then(function () {
90+
return \GuzzleHttp\Promise\coroutine(function () {
91+
yield $promise = new Promise(function () use (&$promise) {
92+
$promise->reject(new \Exception);
93+
});
94+
});
95+
})
96+
->otherwise(function (\Exception $error = null) {
97+
if (!$error) {
98+
self::fail('Error did not propagate.');
99+
}
100+
return 3;
101+
});
102+
103+
$this->assertSame(3, $promise->wait());
104+
}
65105
}

0 commit comments

Comments
 (0)