Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Coroutine.php

class 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::of method 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::of(function () { $value = (yield createPromise('a')); try { $value = (yield createPromise($value . 'b')); } catch (\Throwable $e) { // The promise was rejected. } yield $value . 'c'; });

// Outputs "abc" $promise->then(function ($v) { echo $v; });

Hierarchy

  • class \GuzzleHttp\Promise\Coroutine implements \GuzzleHttp\Promise\PromiseInterface

Expanded class hierarchy of Coroutine

See also

https://github.com/petkaantonov/bluebird/blob/master/API.md#generators inspiration

File

vendor/guzzlehttp/promises/src/Coroutine.php, line 46

Namespace

GuzzleHttp\Promise
View source
final class Coroutine implements PromiseInterface {
    
    /**
     * @var PromiseInterface|null
     */
    private $currentPromise;
    
    /**
     * @var Generator
     */
    private $generator;
    
    /**
     * @var Promise
     */
    private $result;
    public function __construct(callable $generatorFn) {
        $this->generator = $generatorFn();
        $this->result = new Promise(function () : void {
            while (isset($this->currentPromise)) {
                $this->currentPromise
                    ->wait();
            }
        });
        try {
            $this->nextCoroutine($this->generator
                ->current());
        } catch (Throwable $throwable) {
            $this->result
                ->reject($throwable);
        }
    }
    
    /**
     * Create a new coroutine.
     */
    public static function of(callable $generatorFn) : self {
        return new self($generatorFn);
    }
    public function then(?callable $onFulfilled = null, ?callable $onRejected = null) : PromiseInterface {
        return $this->result
            ->then($onFulfilled, $onRejected);
    }
    public function otherwise(callable $onRejected) : PromiseInterface {
        return $this->result
            ->otherwise($onRejected);
    }
    public function wait(bool $unwrap = true) {
        return $this->result
            ->wait($unwrap);
    }
    public function getState() : string {
        return $this->result
            ->getState();
    }
    public function resolve($value) : void {
        $this->result
            ->resolve($value);
    }
    public function reject($reason) : void {
        $this->result
            ->reject($reason);
    }
    public function cancel() : void {
        $this->currentPromise
            ->cancel();
        $this->result
            ->cancel();
    }
    private function nextCoroutine($yielded) : void {
        $this->currentPromise = Create::promiseFor($yielded)->then([
            $this,
            '_handleSuccess',
        ], [
            $this,
            '_handleFailure',
        ]);
    }
    
    /**
     * @internal
     */
    public function _handleSuccess($value) : void {
        unset($this->currentPromise);
        try {
            $next = $this->generator
                ->send($value);
            if ($this->generator
                ->valid()) {
                $this->nextCoroutine($next);
            }
            else {
                $this->result
                    ->resolve($value);
            }
        } catch (Throwable $throwable) {
            $this->result
                ->reject($throwable);
        }
    }
    
    /**
     * @internal
     */
    public function _handleFailure($reason) : void {
        unset($this->currentPromise);
        try {
            $nextYield = $this->generator
                ->throw(Create::exceptionFor($reason));
            // The throw was caught, so keep iterating on the coroutine
            $this->nextCoroutine($nextYield);
        } catch (Throwable $throwable) {
            $this->result
                ->reject($throwable);
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
Coroutine::$currentPromise private property
Coroutine::$generator private property
Coroutine::$result private property
Coroutine::cancel public function Cancels the promise if possible. Overrides PromiseInterface::cancel
Coroutine::getState public function Get the state of the promise ("pending", "rejected", or "fulfilled"). Overrides PromiseInterface::getState
Coroutine::nextCoroutine private function
Coroutine::of public static function Create a new coroutine.
Coroutine::otherwise public function Appends a rejection handler callback to the promise, and returns a new
promise resolving to the return value of the callback if it is called,
or to its original fulfillment value if the promise is instead
fulfilled.
Overrides PromiseInterface::otherwise
Coroutine::reject public function Reject the promise with the given reason. Overrides PromiseInterface::reject
Coroutine::resolve public function Resolve the promise with the given value. Overrides PromiseInterface::resolve
Coroutine::then public function Appends fulfillment and rejection handlers to the promise, and returns
a new promise resolving to the return value of the called handler.
Overrides PromiseInterface::then
Coroutine::wait public function Waits until the promise completes if possible. Overrides PromiseInterface::wait
Coroutine::_handleFailure public function @internal
Coroutine::_handleSuccess public function @internal
Coroutine::__construct public function
PromiseInterface::FULFILLED public constant
PromiseInterface::PENDING public constant
PromiseInterface::REJECTED public constant
RSS feed
Powered by Drupal