commit 3a4b04c1ecec5f63980af13e3ae6c7c20e22c621 Author: Sergio Álvarez Date: Fri Nov 22 20:34:10 2019 +0100 testing promises with php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d1502b0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +vendor/ +composer.lock diff --git a/promises/composer.json b/promises/composer.json new file mode 100644 index 0000000..70b41a8 --- /dev/null +++ b/promises/composer.json @@ -0,0 +1,8 @@ +{ + "require": { + "guzzlehttp/guzzle": "^6.4" + }, + "autoload": { + "files": ["lib.php"] + } +} diff --git a/promises/index.php b/promises/index.php new file mode 100644 index 0000000..c64cba2 --- /dev/null +++ b/promises/index.php @@ -0,0 +1,53 @@ +getAsync($uri . http_build_query(['o' => $i, 's' => random_int(1, 5)])); + }; + } +}; + +Lib\printf('pool'); + +$pool = new Pool($client, $requests(3), [ + 'concurrency' => 5, + 'fulfilled' => function ($response, $index) { + Lib\printf(' i='. $index .' '. $response->getBody()); + }, + 'rejected' => function ($reason, $index) { + Lib\printf(' '. $index .' '. $reason); + }, +]); + +Lib\printf('promise'); +// Initiate the transfers and create a promise +$promise = $pool->promise(); + +Lib\printf('wait'); +// Force the pool of requests to complete. +$promise->wait(); + +Lib\printf('end'); diff --git a/promises/lib.php b/promises/lib.php new file mode 100644 index 0000000..463cd16 --- /dev/null +++ b/promises/lib.php @@ -0,0 +1,24 @@ +