1
0
mirror of https://github.com/danog/amp.git synced 2024-12-11 17:09:40 +01:00
amp/lib/Coroutine.php
Aaron Piotrowski b65823e0bb
Refactor Coroutine to use await()
Dropped polyfill, ext-fiber will be required to run any v3 code.
2020-09-26 22:22:07 -05:00

46 lines
1.4 KiB
PHP

<?php
namespace Amp;
/**
* @deprecated Use {@see await()} and ext-fiber to await promises.
*
* Creates a promise from a generator function yielding promises.
*
* When a promise is yielded, execution of the generator is interrupted until the promise is resolved. A success
* value is sent into the generator, while a failure reason is thrown into the generator. Using a coroutine,
* asynchronous code can be written without callbacks and be structured like synchronous code.
*
* @template-covariant TReturn
* @template-implements Promise<TReturn>
*/
final class Coroutine implements Promise
{
use Internal\Placeholder;
/**
* @param \Generator $generator
* @psalm-param \Generator<mixed,Promise|ReactPromise|array<array-key,
* Promise|ReactPromise>,mixed,Promise<TReturn>|ReactPromise|TReturn> $generator
*/
public function __construct(\Generator $generator)
{
$this->resolve(async(function () use ($generator): mixed {
$yielded = $generator->current();
while ($generator->valid()) {
try {
$value = await($yielded);
} catch (\Throwable $exception) {
$yielded = $generator->throw($exception);
continue;
}
$yielded = $generator->send($value);
}
return $generator->getReturn();
}));
}
}