fiber-interop

fiber\run()

The only entry point: run $f and wait for every task it started.

function fiber\run(callable $f, mixed ...$args): mixed

Behaviour

Structured: run() runs $f as a task, then waits until every task started inside it (transitively, except service() tasks) has finished. It rethrows $f's exception, else the first exception of a task nobody awaited, else returns $f's result.

It nests freely and behaves the same in {main} with no loop running as inside a task. Every other function in this API throws NotInRunException when called outside it (except setup(), register() and adapter()).

Example

Source: examples/api/run.php. Run on phasync and Revolt; the output is identical.

<?php
require __DIR__ . '/vendor/autoload.php';
use function fiber\{run, go, sleep};
$r = run(function () {
    go(function () { sleep(0.1); echo "child finished\n"; }); // nobody awaits it
    return 'body returned';
});
echo $r, "\n"; // printed after "child finished": run() drains its tasks
try {
    fiber\sleep(0.01); // outside run()
} catch (fiber\NotInRunException) {
    echo "NotInRunException outside run()\n";
}

All functions