function coroutine in Zircon Profile 8
Same name and namespace in other branches
- 8.0 vendor/guzzlehttp/promises/src/functions.php \GuzzleHttp\Promise\coroutine()
Creates a promise that is resolved using a generator that yields values or promises (somewhat similar to C#'s async keyword).
When called, the coroutine function will start an instance of the generator and returns a promise that is fulfilled with its final yielded value.
Control is returned back to the generator when the yielded promise settles. This can lead to less verbose code when doing lots of sequential async calls with minimal processing in between.
use GuzzleHttp\Promise;
function createPromise($value) { return new Promise\FulfilledPromise($value); }
$promise = Promise\coroutine(function () { $value = (yield createPromise('a')); try { $value = (yield createPromise($value . 'b')); } catch (\Exception $e) { // The promise was rejected. } yield $value . 'c'; });
// Outputs "abc" $promise->then(function ($v) { echo $v; });
@link https://github.com/petkaantonov/bluebird/blob/master/API.md#generators inspiration
Parameters
callable $generatorFn Generator function to wrap into a promise.:
Return value
File
- vendor/
guzzlehttp/ promises/ src/ functions.php, line 473
Namespace
GuzzleHttp\PromiseCode
function coroutine(callable $generatorFn) {
$generator = $generatorFn();
return __next_coroutine($generator
->current(), $generator)
->then();
}