50 if (!$generator instanceof Generator) {
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) {
76 if ($reason instanceof Exception) {
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));
97 $lastYieldResult = $yieldedValue;
98 $generator->send($yieldedValue);
107 $promise->fulfill($lastYieldResult);
114 }
catch (Exception $e) {
An exception for terminatinating execution or to throw for unit testing.
An implementation of the Promise pattern.
const PENDING
The asynchronous operation is pending.
error($a_errmsg)
set error message @access public
coroutine(callable $gen)
Turn asynchronous promise-based code into something that looks synchronous again, through the use of ...
$promise
This example shows demonstrates the Promise api.