update docs for 2.1
[mdref/mdref-pq-gateway] / pq-gateway / pq / Query / AsyncExecutor.md
index d7804d61e0f5f1d9ce33ad0158ee49f71dbc84e2..c4b8afab65d6e563bc07394c147acb5d27e73ed2 100644 (file)
@@ -3,5 +3,76 @@
 An asynchronous query executor implementation.
 See pq\Query\Executor for inherited methods and properties.
 
-> ***NOTE:***  
-  This asynchronous executor implementation depends on [React/Promise](https://github.com/reactphp/promise).
+## Example with [React/Promise](https://github.com/reactphp/promise)
+
+       <?php
+       
+       use pq\Connection, pq\Result;
+       use pq\Query\AsyncExecutor, pq\Query\Writer;
+       
+       use React\Promise\Deferred;
+       
+       $conn = new Connection;
+       $exec = new AsyncExecutor($conn);
+       $exec->setCallbacks(
+       # init context
+       function() {
+               return new Deferred;
+       },
+       # done
+       function(Deferred $context, $result) {
+               $context->resolve($result);
+       },
+       # then
+       function(Deferred $context, callable $cb) {
+               return $context->promise()->then($cb);
+       });
+
+       # a contrived query
+       $query = new Writer("SELECT \$1::int, \$2::int", [1, 2]);
+       $exec->execute($query, function(Result $result) {
+               var_dump($result->fetchAll());
+       });
+       
+       # this call blocks; see pq\Connection::getResult() etc.
+       $conn->getResult();
+       
+       ?>
+
+## Example with [Amphp/Amp](https://github.com/amphp/amp)
+
+       <?php
+       
+       use pq\Connection, pq\Result;
+       use pq\Query\AsyncExecutor, pq\Query\Writer;
+       
+       use Amphp\Deferred;
+       
+       $conn = new Connection;
+       $exec = new AsyncExecutor($conn);
+       $exec->setCallbacks(
+       # init context
+       function() {
+               return new Deferred;
+       },
+       # done
+       function(Deferred $context, $result) {
+               $context->succeed($result);
+       },
+       # then
+       function(Deferred $context, callable $cb) {
+               return $context->promise()->when(function($error, $result) use ($cb) {
+                       $cb($result);
+               });
+       });
+       
+       # a contrived query
+       $query = new Writer("SELECT \$1::int, \$2::int", [1, 2]);
+       $exec->execute($query, function(Result $result) {
+               var_dump($result->fetchAll());
+       });
+       
+       # this call blocks; see pq\Connection::getResult() etc.
+       $conn->getResult();
+       
+       ?>