|  | 
| 1 | 1 | Promise | 
| 2 | 2 | ======= | 
| 3 | 3 | 
 | 
| 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. | 
| 7 | 7 | 
 | 
| 8 | 8 | .. note:: | 
| 9 | 9 | 
 | 
| 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 | +    ); | 
| 12 | 69 | 
 | 
| 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 | 
0 commit comments