Future::await
(PHP 8.6+, True Async 1.0)
php
public function await(?Completable $cancellation = null): mixedAwaits the completion of the Future and returns its result. Blocks the current coroutine until the Future is completed. If the Future completed with an error, the method throws that exception. You can pass a Completable to cancel the wait by timeout or external condition.
Parameters
cancellation — a wait cancellation object. If provided and triggered before the Future completes, a AsyncCancellation will be thrown. Defaults to null.
Return value
mixed — the Future result.
Errors
Throws an exception if the Future completed with an error or was cancelled.
Examples
Example #1 Basic result awaiting
php
<?php
use Async\Future;
$future = \Async\async(function() {
\Async\delay(100);
return 42;
});
$result = $future->await();
echo "Result: $result\n"; // Result: 42Example #2 Handling errors during await
php
<?php
use Async\Future;
$future = \Async\async(function() {
throw new \RuntimeException("Something went wrong");
});
try {
$result = $future->await();
} catch (\RuntimeException $e) {
echo "Error: " . $e->getMessage() . "\n";
// Error: Something went wrong
}See also
- Future::isCompleted — Check if the Future is completed
- Future::cancel — Cancel the Future
- Future::map — Transform the result