fiber\await()
Wait for an awaitable and return its result.
function fiber\await(object $awaitable, float $timeout = PHP_FLOAT_MAX): mixed
Behaviour
Accepts awaitables from go(), any object with then(callable, callable), and whatever the adapter's canAwait() accepts; anything else is a TypeError. Several fibers may await the same awaitable, and it can be awaited repeatedly. Outside run() it throws NotInRunException.
An exception thrown by the task is rethrown unchanged. A non-Throwable rejection reason is wrapped in RejectedException. On timeout a TimeoutException is thrown; the task keeps running and its later result is ignored (there is no cancellation of the task).
From a fiber the adapter did not create (and not {main}) the adapter runs its loop in place instead of suspending that fiber (the in-place rule); how that is done differs per runtime, see the runtime pages.
Example
<?php
require __DIR__ . '/vendor/autoload.php';
use function fiber\{run, go, await, sleep};
use fiber\TimeoutException;
echo run(fn() => await(go(fn() => 6 * 7))), "\n";
run(function () {
$t = go(function () { sleep(0.3); return 'done'; });
try {
await($t, 0.05);
} catch (TimeoutException) {
echo "timed out; task still running\n";
}
echo await($t), "\n"; // the same awaitable can be awaited again
});