51 throw new \InvalidArgumentException(
'You must pass a generator function');
57 $lastYieldResult = null;
63 $advanceGenerator =
function() use (&$advanceGenerator, $generator,
$promise, &$lastYieldResult) {
65 while ($generator->valid()) {
67 $yieldedValue = $generator->current();
68 if ($yieldedValue instanceof
Promise) {
70 function($value) use ($generator, &$advanceGenerator, &$lastYieldResult) {
71 $lastYieldResult = $value;
72 $generator->send($value);
75 function($reason) use ($generator, $advanceGenerator) {
77 $generator->throw($reason);
78 } elseif (is_scalar($reason)) {
79 $generator->throw(
new Exception($reason));
81 $type = is_object($reason) ? get_class($reason) : gettype($reason);
82 $generator->throw(
new Exception(
'Promise was rejected with reason of type: ' .
$type));
86 )->error(
function($reason) use (
$promise) {
97 $lastYieldResult = $yieldedValue;
98 $generator->send($yieldedValue);
107 $promise->fulfill($lastYieldResult);
An implementation of the Promise pattern.
$promise
This example shows demonstrates the Promise api.
coroutine(callable $gen)
Turn asynchronous promise-based code into something that looks synchronous again, through the use of ...
const PENDING
The asynchronous operation is pending.