phasync
phasync / go

Method

phasync::go

Starts $fn as a coroutine in the current frame and scope, and returns it, without waiting for it.

Description#

public static phasync::go(
    callable $fn,
    mixed ...$args
): Fiber

The coroutine runs at once, up to its first wait, and then go() returns it: the caller's next statement sees what the coroutine did before it waited. It belongs to the frame of the run() the caller is in, which does not return before it has ended, and runs in the Scope current where go() is called (Scope::getCurrent() inside it returns that scope, also after it waited). Pass the result to await() for the return value or the exception; an exception nobody awaits is thrown by the run(), see phasync::run.

Parameters#

$fn callable
The coroutine
...$args mixed
Passed to $fn

Return values#

The coroutine, for await(), cancel() and throw() Fiber

Errors / Exceptions#

ExceptionWhen
LogicExceptionOutside a coroutine

Examples#

Example 1
phasync::run(function () {
    $fiber = phasync::go(function () {
        echo "child\n";
        phasync::sleep(0.1);
        return 42;
    });
    echo "parent\n";                // after "child": it ran up to its first wait
    echo phasync::await($fiber), "\n";   // 42
});

See also#

  • phasync::runRuns $fn and returns its result once it and every coroutine started inside it have ended.
  • phasync::awaitWaits for a coroutine or a promise, and returns its result or throws the exception it ended with.
  • phasync::serviceStarts $fn as a service: a coroutine of no frame, in the root scope, which outlives the code that started it, and returns it.
  • WaitGroupTo wait for many