Skip to content

Fix promise unwrapping in tutorial #172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 22, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions httplug/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ Using the promise directly
If you don't want to use the callback system, you can also get the state of the promise with ``$promise->getState()``
will return of one ``Promise::PENDING``, ``Promise::FULFILLED`` or ``Promise::REJECTED``.

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

.. note::

Expand All @@ -131,6 +131,7 @@ Example

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

use Http\Client\Exception;
use Http\Discovery\HttpAsyncClientDiscovery;

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

// We need now the response for our final treatment
$promise->wait();

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

// Do your stuff with the response
Expand Down