Future::cancel
(PHP 8.6+, True Async 1.0)
public function cancel(?AsyncCancellation $cancellation = null): void
Annulla il Future. Tutte le coroutine in attesa di questo Future tramite await() riceveranno una CancelledException. Se viene fornito il parametro $cancellation, verra’ utilizzato come motivo dell’annullamento.
Parametri
cancellation — un’eccezione di annullamento personalizzata. Se null, viene utilizzata la CancelledException predefinita.
Valore di ritorno
La funzione non restituisce alcun valore.
Esempi
Esempio #1 Annullamento base del Future
<?php
use Async\Future;
use Async\FutureState;
$state = new FutureState();
$future = new Future($state);
// Una coroutine in attesa del risultato
\Async\async(function() use ($future) {
try {
$result = $future->await();
} catch (\Async\CancelledException $e) {
echo "Future cancelled\n";
}
});
// Annulla il Future
$future->cancel();
Esempio #2 Annullamento con motivo personalizzato
<?php
use Async\Future;
use Async\FutureState;
use Async\AsyncCancellation;
$state = new FutureState();
$future = new Future($state);
\Async\async(function() use ($future) {
try {
$future->await();
} catch (\Async\CancelledException $e) {
echo "Reason: " . $e->getMessage() . "\n";
// Reason: Timeout exceeded
}
});
$future->cancel(new AsyncCancellation("Timeout exceeded"));
Vedi anche
- Future::isCancelled — Verifica se il Future e’ annullato
- Future::await — Attende il risultato
- Future::catch — Gestisce gli errori del Future