Skip to content

Commit 083ccf6

Browse files
authored
Merge pull request #172 from php-http/promise_fixes
Fix promise unwrapping in tutorial
2 parents 47d223c + f73a36d commit 083ccf6

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

httplug/tutorial.rst

+8-9
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ Using the promise directly
119119
If you don't want to use the callback system, you can also get the state of the promise with ``$promise->getState()``
120120
will return of one ``Promise::PENDING``, ``Promise::FULFILLED`` or ``Promise::REJECTED``.
121121

122-
Then you can get the response of the promise if it's in ``FULFILLED`` state with ``$promise->getResponse()`` call or
123-
get the error of the promise if it's in ``REJECTED`` state with ``$promise->getRequest()`` call
122+
Then you can get the response of the promise if it's in ``FULFILLED`` state or trigger the exception of the promise
123+
if it's in ``REJECTED`` state with ``$promise->wait(true)`` call.
124124

125125
.. note::
126126

@@ -131,6 +131,7 @@ Example
131131

132132
Here is a full example of a classic usage when using the ``sendAsyncRequest`` method::
133133

134+
use Http\Client\Exception;
134135
use Http\Discovery\HttpAsyncClientDiscovery;
135136

136137
$httpAsyncClient = HttpAsyncClientDiscovery::find();
@@ -149,13 +150,11 @@ Here is a full example of a classic usage when using the ``sendAsyncRequest`` me
149150
// Do some stuff not depending on the response, calling another request, etc ..
150151
...
151152

152-
// We need now the response for our final treatment
153-
$promise->wait();
154-
155-
if (Promise::FULFILLED === $promise->getState()) {
156-
$response = $promise->getResponse();
157-
} else {
158-
throw new \Exception('Response not available');
153+
try {
154+
// We need now the response for our final treatment...
155+
$response = $promise->wait(true);
156+
} catch (Exception $e) {
157+
// ...or catch the thrown exception
159158
}
160159

161160
// Do your stuff with the response

0 commit comments

Comments
 (0)