/** * {@inheritdoc} */ public function resolve() { $this->promise->wait(); }
public function testDoesNotBlowStackWhenWaitingOnNestedThens() { $inner = new Promise(function () use(&$inner) { $inner->resolve(0); }); $prev = $inner; for ($i = 1; $i < 100; $i++) { $prev = $prev->then(function ($i) { return $i + 1; }); } $parent = new Promise(function () use(&$parent, $prev) { $parent->resolve($prev); }); $this->assertEquals(99, $parent->wait()); }
$results = GuzzleHttp\Promise\unwrap($promises); foreach ($results as $k => $result) { echo $k . " " . $result->getBody() . "\n"; } // So in your app you build of an array of async requests at the top, wait from them all to complete and build the result // This isn't the only model, but it is the most straight forward, since it lets you thing about things // in a linear way // You can also push the promises throughout the code, and then just wait on them when you need the response // if you use promises to there full extent you can even handle the generic api response handling to // make a fully async api $usernamePromise = $usernameClient->postAsync('generate-username'); $username = new Promise(function () use($usernamePromise, &$username) { $data = json_decode($usernamePromise->wait()->getBody()); $username->resolve($data->username); }); echo "Username: {$username->wait()}\n"; // if you wanted to pass things all the way into a template you could do a trick like use GuzzleHttp\Promise\PromiseInterface; class StringWaiter { private $promise; public function __construct(PromiseInterface $promise) { $this->promise = $promise; } public function __toString() { try { return $this->promise->wait(); } catch (Exception $e) { return "Error: " . $e->getMessage() . "\n";
/** * @param Promise $promise */ protected function wait(Promise $promise) { $promise->wait(); }
public function wait($unwrap = true) { return $this->result->wait($unwrap); }