Method
phasync::throw
Interrupts the wait of a suspended coroutine with $exception, once.
Description#
public static phasync::throw(
Fiber $fiber,
Throwable $exception
): voidThe 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#
| Exception | When |
|---|---|
InvalidArgumentException | If the coroutine has ended |
LogicException | If the coroutine is not waiting |
Examples#
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#
phasync::cancelA sticky cancellation