coroutine.php
Table of Contents
- coroutine() : Promise
- Turn asynchronous promise-based code into something that looks synchronous again, through the use of generators.
Functions
coroutine()
Turn asynchronous promise-based code into something that looks synchronous again, through the use of generators.
coroutine(callable $gen) : Promise
Example without coroutines:
$promise = $httpClient->request('GET', '/foo'); $promise->then(function($value) {
return $httpClient->request('DELETE','/foo');
})->then(function($value) {
return $httpClient->request('PUT', '/foo');
})->error(function($reason) {
echo "Failed because: $reason\n";
});
Example with coroutines:
coroutine(function() {
try { yield $httpClient->request('GET', '/foo'); yield $httpClient->request('DELETE', /foo'); yield $httpClient->request('PUT', '/foo'); } catch(\Throwable $reason) { echo "Failed because: $reason\n"; }
});
Parameters
- $gen : callable