phasync
phasync / throw

Method

phasync::throw

Interrupts the wait of a suspended coroutine with $exception, once.

Description#

public static phasync::throw(
    Fiber $fiber,
    Throwable $exception
): void

The coroutine can catch it and carry on: nothing is remembered, and its next wait is an ordinary wait. A coroutine that ends with the exception is no failure of the run, as with phasync::cancel. The coroutine must be waiting (in await(), sleep(), readable(), awaitFlag() and the like) and not be shielded, such as inside a finally() callback. Throwing into the calling coroutine throws at once.

Parameters#

$fiber Fiber
$exception Throwable

Errors / Exceptions#

ExceptionWhen
InvalidArgumentExceptionIf the coroutine has ended
LogicExceptionIf the coroutine is not waiting

Examples#

Example 1
phasync::run(function () {
    $fiber = phasync::go(function () {
        try {
            phasync::sleep(10);
        } catch (RuntimeException $e) {
            echo "interrupted: ", $e->getMessage(), "\n";
        }
        phasync::sleep(0.01);   // an ordinary wait again
        return 'carried on';
    });
    phasync::throw($fiber, new RuntimeException('wake up'));
    echo phasync::await($fiber), "\n";
});

See also#