\GuzzleHttp\PromiseCoroutine

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 (\Exception $e) {
        // The promise was rejected.
    }
    yield $value . 'c';
});

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

Summary

Methods
Properties
Constants
__construct()
of()
then()
otherwise()
wait()
getState()
resolve()
reject()
cancel()
No public properties found
No constants found
No protected methods found
No protected properties found
N/A
nextCoroutine()
$currentPromise
$generator
$result
N/A

Properties

$currentPromise

$currentPromise : \GuzzleHttp\Promise\PromiseInterface|null

Type

PromiseInterface|null

$generator

$generator : \Generator

Type

\Generator —

$result

$result : \GuzzleHttp\Promise\Promise

Type

Promise

Methods

__construct()

__construct(callable  $generatorFn) : mixed

Parameters

callable $generatorFn

Returns

mixed —

of()

of(callable  $generatorFn) : self

Create a new coroutine.

Parameters

callable $generatorFn

Returns

self —

then()

then(callable  $onFulfilled = null, callable  $onRejected = null) : \GuzzleHttp\Promise\PromiseInterface

Appends fulfillment and rejection handlers to the promise, and returns a new promise resolving to the return value of the called handler.

Parameters

callable $onFulfilled

Invoked when the promise fulfills.

callable $onRejected

Invoked when the promise is rejected.

Returns

\GuzzleHttp\Promise\PromiseInterface —

otherwise()

otherwise(callable  $onRejected) : \GuzzleHttp\Promise\PromiseInterface

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.

Parameters

callable $onRejected

Invoked when the promise is rejected.

Returns

\GuzzleHttp\Promise\PromiseInterface —

wait()

wait(mixed  $unwrap = true) : mixed

Waits until the promise completes if possible.

Pass $unwrap as true to unwrap the result of the promise, either returning the resolved value or throwing the rejected exception.

If the promise cannot be waited on, then the promise will be rejected.

Parameters

mixed $unwrap

Returns

mixed —

getState()

getState() : string

Get the state of the promise ("pending", "rejected", or "fulfilled").

The three states can be checked against the constants defined on PromiseInterface: PENDING, FULFILLED, and REJECTED.

Returns

string —

resolve()

resolve(mixed  $value) : mixed

Resolve the promise with the given value.

Parameters

mixed $value

Returns

mixed —

reject()

reject(mixed  $reason) : mixed

Reject the promise with the given reason.

Parameters

mixed $reason

Returns

mixed —

cancel()

cancel() : mixed

Cancels the promise if possible.

Returns

mixed —

nextCoroutine()

nextCoroutine(mixed  $yielded) : mixed

Parameters

mixed $yielded

Returns

mixed —