phasync
Frame / cancel

Method

Frame::cancel

Cancels every coroutine of the frame, and of the frames nested in it, with a CancelledException.

Description#

public Frame::cancel(
    Stringable|CancelledException|string $message = 'Operation cancelled'
): void

As phasync::cancel does for one coroutine: the cancellation is sticky, so every wait of a coroutine in the frame throws it, until the coroutine ends or the nested run() it is in returns. The deepest in the tree of coroutines are cancelled first. It throws nothing in the caller: a coroutine that is running, and not waiting, meets it at its next wait, the caller too when it is in the frame. A coroutine that ends with the cancellation is no failure, so run() throws it only when $fn does. It also requests cancellation (Frame::requestCancellation).

Parameters#

$message Stringable|CancelledException|string = 'Operation cancelled'
The message of the CancelledException, or the exception itself

Errors / Exceptions#

ExceptionWhen
LogicExceptionWhen the frame's run() has returned

Examples#

Example 1
phasync::run(function () {
    phasync::go(fn () => phasync::sleep(10));
    phasync::go(fn () => phasync::sleep(10));
    phasync::frame()->cancel();   // returns: the main coroutine is in the frame
    phasync::sleep(1);            // throws phasync\CancelledException
});

See also#