Skip to content

Commit 7859c1e

Browse files
committed
Merge pull request #68 from php-http/promise
Document Promise
2 parents 1f11fa4 + 66f72f5 commit 7859c1e

File tree

2 files changed

+68
-6
lines changed

2 files changed

+68
-6
lines changed

Diff for: components/promise.rst

+64-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,71 @@
11
Promise
22
=======
33

4-
Promise used for asynchronous HTTP requests.
5-
6-
This will eventually be removed/deprecated and replaced with the upcoming Promise PSR.
4+
When sending asynchronous HTTP requests, a promise is returned. The promise acts
5+
as a proxy for the response or error result, which is not yet known.
6+
The PHP-HTTP promise follows the `Promises/A+`_ standard.
77

88
.. note::
99

10-
Contract for the ``Http\Promise\Promise`` is temporary until `PSR is released`_.
11-
Once it is out, we will use this PSR in the main client and deprecate the old contract.
10+
Work is underway for a `Promise PSR`_. When that PSR has been released, we
11+
will use it in HTTPlug and deprecate our ``Http\Promise\Promise`` interface.
12+
13+
Asynchronous requests
14+
---------------------
15+
16+
Asynchronous requests enable non-blocking HTTP operations. To execute such a
17+
request with HTTPlug::
18+
19+
$request = $messageFactory->createRequest('GET', 'http://php-http.org');
20+
21+
// Where $client implements HttpAsyncClient
22+
$promise = $client->sendAsyncRequest($request);
23+
24+
// This code will be executed right after the request is sent, but before
25+
// the response is returned.
26+
echo 'Wow, non-blocking!';
27+
28+
See :ref:`message-factory` on how to use message factories.
29+
30+
Wait
31+
----
32+
33+
The ``$promise`` that is returned implements ``Http\Promise\Promise``. At this
34+
point in time, the response is not known yet. You can be polite and wait for
35+
that response to arrive::
36+
37+
try {
38+
$response = $promise->wait();
39+
} catch (\Exception $exception) {
40+
echo $exception->getMessage();
41+
}
42+
43+
Then
44+
----
45+
46+
Instead of waiting, however, you can handle things asynchronously. Call the
47+
``then`` method with two arguments: one callback that will be executed if the
48+
request turns out to be successful and/or a second callback that will be
49+
executed if the request results in an error::
50+
51+
$promise->then(
52+
// The success callback
53+
function (ResponseInterface $response) {
54+
echo 'Yay, we have a shiny new response!';
55+
56+
// Write status code to some log file
57+
file_put_contents('responses.log', $response->getStatusCode() . "\n", FILE_APPEND);
58+
59+
return $response;
60+
},
61+
62+
// The failure callback
63+
function (\Exception $exception) {
64+
echo 'Oh darn, we have a problem';
65+
66+
throw $exception;
67+
}
68+
);
1269

13-
.. _`PSR is released`: https://groups.google.com/forum/?fromgroups#!topic/php-fig/wzQWpLvNSjs
70+
.. _`Promise PSR`: https://groups.google.com/forum/?fromgroups#!topic/php-fig/wzQWpLvNSjs
71+
.. _Promises/A+: https://promisesaplus.com

Diff for: httplug/tutorial.rst

+4
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ will return of one ``Promise::PENDING``, ``Promise::FULFILLED`` or ``Promise::RE
116116
Then you can get the response of the promise if it's in ``FULFILLED`` state with ``$promise->getResponse()`` call or
117117
get the error of the promise if it's in ``REJECTED`` state with ``$promise->getRequest()`` call
118118

119+
.. note::
120+
121+
Read :doc:`/components/promise` for more information about promises.
122+
119123
Example
120124
^^^^^^^
121125

0 commit comments

Comments
 (0)