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'
): voidAs 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#
| Exception | When |
|---|---|
LogicException | When the frame's run() has returned |
Examples#
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#
phasync::cancelCancels a coroutine with a CancelledException.Frame::requestCancellationWithout an exceptionFrame::setTimeoutCancels the frame when$secondshave passed, and makes itsrun()throw a TimeoutException instead of the cancellation.