Open Journal Systems
3.3.0
|
Public Member Functions | |
__construct (callable $generatorFn) | |
_handleFailure ($reason) | |
_handleSuccess ($value) | |
cancel () | |
getState () | |
otherwise (callable $onRejected) | |
reject ($reason) | |
resolve ($value) | |
then (callable $onFulfilled=null, callable $onRejected=null) | |
wait ($unwrap=true) | |
Additional Inherited Members | |
![]() | |
const | FULFILLED = 'fulfilled' |
const | PENDING = 'pending' |
const | REJECTED = 'rejected' |
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; });
callable | $generatorFn | Generator function to wrap into a promise. |
Definition at line 43 of file Coroutine.php.
GuzzleHttp\Promise\Coroutine::__construct | ( | callable | $generatorFn | ) |
Definition at line 69 of file Coroutine.php.
GuzzleHttp\Promise\Coroutine::_handleFailure | ( | $reason | ) |
Definition at line 147 of file Coroutine.php.
References GuzzleHttp\Promise\exception_for().
GuzzleHttp\Promise\Coroutine::_handleSuccess | ( | $value | ) |
Definition at line 127 of file Coroutine.php.
GuzzleHttp\Promise\Coroutine::cancel | ( | ) |
Cancels the promise if possible.
https://github.com/promises-aplus/cancellation-spec/issues/7
Implements GuzzleHttp\Promise\PromiseInterface.
Definition at line 112 of file Coroutine.php.
GuzzleHttp\Promise\Coroutine::getState | ( | ) |
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.
Implements GuzzleHttp\Promise\PromiseInterface.
Definition at line 97 of file Coroutine.php.
GuzzleHttp\Promise\Coroutine::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.
callable | $onRejected | Invoked when the promise is rejected. |
Implements GuzzleHttp\Promise\PromiseInterface.
Definition at line 87 of file Coroutine.php.
GuzzleHttp\Promise\Coroutine::reject | ( | $reason | ) |
Reject the promise with the given reason.
mixed | $reason |
Implements GuzzleHttp\Promise\PromiseInterface.
Definition at line 107 of file Coroutine.php.
GuzzleHttp\Promise\Coroutine::resolve | ( | $value | ) |
Resolve the promise with the given value.
mixed | $value |
Implements GuzzleHttp\Promise\PromiseInterface.
Definition at line 102 of file Coroutine.php.
GuzzleHttp\Promise\Coroutine::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.
callable | $onFulfilled | Invoked when the promise fulfills. |
callable | $onRejected | Invoked when the promise is rejected. |
Implements GuzzleHttp\Promise\PromiseInterface.
Definition at line 80 of file Coroutine.php.
GuzzleHttp\Promise\Coroutine::wait | ( | $unwrap = true | ) |
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.
bool | $unwrap |
Implements GuzzleHttp\Promise\PromiseInterface.
Definition at line 92 of file Coroutine.php.