Coroutine
in package
implements
PromiseInterface
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; });
Tags
Interfaces, Classes and Traits
- PromiseInterface
- A promise represents the eventual result of an asynchronous operation.
Table of Contents
- $currentPromise : PromiseInterface|null
- $generator : Generator
- $result : Promise
- __construct() : mixed
- cancel() : mixed
- Cancels the promise if possible.
- getState() : string
- Get the state of the promise ("pending", "rejected", or "fulfilled").
- of() : self
- Create a new coroutine.
- otherwise() : 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.
- reject() : mixed
- Reject the promise with the given reason.
- resolve() : mixed
- Resolve the promise with the given value.
- then() : PromiseInterface
- Appends fulfillment and rejection handlers to the promise, and returns a new promise resolving to the return value of the called handler.
- wait() : mixed
- Waits until the promise completes if possible.
- nextCoroutine() : mixed
Properties
$currentPromise
private
PromiseInterface|null
$currentPromise
$generator
private
Generator
$generator
$result
private
Promise
$result
Methods
__construct()
public
__construct(callable $generatorFn) : mixed
Parameters
- $generatorFn : callable
Return values
mixed —cancel()
Cancels the promise if possible.
public
cancel() : mixed
Return values
mixed —getState()
Get the state of the promise ("pending", "rejected", or "fulfilled").
public
getState() : string
The three states can be checked against the constants defined on PromiseInterface: PENDING, FULFILLED, and REJECTED.
Return values
string —of()
Create a new coroutine.
public
static of(callable $generatorFn) : self
Parameters
- $generatorFn : callable
Return values
self —otherwise()
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.
public
otherwise(callable $onRejected) : PromiseInterface
Parameters
- $onRejected : callable
-
Invoked when the promise is rejected.
Return values
PromiseInterface —reject()
Reject the promise with the given reason.
public
reject(mixed $reason) : mixed
Parameters
- $reason : mixed
Return values
mixed —resolve()
Resolve the promise with the given value.
public
resolve(mixed $value) : mixed
Parameters
- $value : mixed
Return values
mixed —then()
Appends fulfillment and rejection handlers to the promise, and returns a new promise resolving to the return value of the called handler.
public
then([callable $onFulfilled = null ][, callable $onRejected = null ]) : PromiseInterface
Parameters
- $onFulfilled : callable = null
-
Invoked when the promise fulfills.
- $onRejected : callable = null
-
Invoked when the promise is rejected.
Return values
PromiseInterface —wait()
Waits until the promise completes if possible.
public
wait([mixed $unwrap = true ]) : mixed
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
- $unwrap : mixed = true
Return values
mixed —nextCoroutine()
private
nextCoroutine(mixed $yielded) : mixed
Parameters
- $yielded : mixed