Future::cancel
(PHP 8.6+, True Async 1.0)
php
public function cancel(?AsyncCancellation $cancellation = null): voidAnnulla il Future. Tutte le coroutine in attesa di questo Future tramite await() riceveranno una AsyncCancellation. Se viene fornito il parametro $cancellation, verra' utilizzato come motivo dell'annullamento.
Parametri
cancellation --- un'eccezione di annullamento personalizzata. Se null, viene utilizzata la AsyncCancellation predefinita.
Valore di ritorno
La funzione non restituisce alcun valore.
Esempi
Esempio #1 Annullamento base del Future
php
<?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\AsyncCancellation $e) {
echo "Future cancelled\n";
}
});
// Annulla il Future
$future->cancel();Esempio #2 Annullamento con motivo personalizzato
php
<?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\AsyncCancellation $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