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 function 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(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; });
-
GuzzleHttp\Promise\Coroutine
implements
GuzzleHttp\Promise\PromiseInterface
Methods summary
public
|
|
public
GuzzleHttp\Promise\PromiseInterface
|
#
then( callable $onFulfilled = null, callable $onRejected = null )
Appends fulfillment and rejection handlers to the promise, and returns
a new promise resolving to the return value of the called handler.
Appends fulfillment and rejection handlers to the promise, and returns
a new promise resolving to the return value of the called handler.
Parameters
- $onFulfilled
- Invoked when the promise fulfills.
- $onRejected
- Invoked when the promise is rejected.
Returns
Implementation of
|
public
GuzzleHttp\Promise\PromiseInterface
|
#
otherwise( callable $onRejected )
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.
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
- $onRejected
- Invoked when the promise is rejected.
Returns
Implementation of
|
public
mixed
|
#
wait( boolean $unwrap = true )
Waits until the promise completes if possible.
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
Returns
mixed
Throws
LogicException if the promise has no wait function or if the
promise does not settle after waiting.
Implementation of
|
public
string
|
#
getState( )
Get the state of the promise ("pending", "rejected", or "fulfilled").
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
Implementation of
|
public
|
#
resolve( mixed $value )
Resolve the promise with the given value.
Resolve the promise with the given value.
Parameters
Throws
RuntimeException if the promise is already resolved.
Implementation of
|
public
|
#
reject( mixed $reason )
Reject the promise with the given reason.
Reject the promise with the given reason.
Parameters
Throws
RuntimeException if the promise is already resolved.
Implementation of
|
public
|
#
cancel( )
Cancels the promise if possible.
Cancels the promise if possible.
Link
Implementation of
|