Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

(no description)

File Size: 169 lines (4 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class

Coroutine:: (12 methods):
  __construct()
  of()
  then()
  otherwise()
  wait()
  getState()
  resolve()
  reject()
  cancel()
  nextCoroutine()
  _handleSuccess()
  _handleFailure()


Class: Coroutine  - X-Ref

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; });

__construct(callable $generatorFn)   X-Ref


of(callable $generatorFn)   X-Ref
Create a new coroutine.

return: self

then(callable $onFulfilled = null,callable $onRejected = null)   X-Ref
No description

otherwise(callable $onRejected)   X-Ref
No description

wait($unwrap = true)   X-Ref
No description

getState()   X-Ref
No description

resolve($value)   X-Ref
No description

reject($reason)   X-Ref
No description

cancel()   X-Ref
No description

nextCoroutine($yielded)   X-Ref
No description

_handleSuccess($value)   X-Ref


_handleFailure($reason)   X-Ref