Copied!

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; });
  • param callable $generatorFn Generator function to wrap into a promise.
  • return \Promise
  • see https://github.com/petkaantonov/bluebird/blob/master/API.md#generatorsinspiration
CloneableFinalInstantiable
Constants
public GuzzleHttp\Promise\PromiseInterface::FULFILLED = 'fulfilled'
public GuzzleHttp\Promise\PromiseInterface::PENDING = 'pending'
public GuzzleHttp\Promise\PromiseInterface::REJECTED = 'rejected'
Methods
public __construct(callable $generatorFn)
public _handleFailure( $reason) : void
 
  • internal
public _handleSuccess( $value) : void
 
  • internal
public cancel() : void
public getState() : string
public static of(callable $generatorFn) : self
 

Create a new coroutine.

public otherwise(callable $onRejected) : GuzzleHttp\Promise\PromiseInterface
public reject( $reason) : void
public resolve( $value) : void
public then(?callable $onFulfilled = NULL, ?callable $onRejected = NULL) : GuzzleHttp\Promise\PromiseInterface
public wait(bool $unwrap = true)
Properties
private $currentPromise = NULL
 
  • var \PromiseInterface|null
private $generator = NULL
 
  • var \Generator
private $result = NULL
 
  • var \Promise
Methods
private nextCoroutine( $yielded) : void
Methods
public static of(callable $generatorFn) : self
 

Create a new coroutine.

© 2024 Bruce Wells
Search Namespaces \ Classes
Configuration