\Co\cancel
###API
namespace Co;
function cancel(string $eventId): string;
Parameter descriptionβ
Parameters | Type | Description |
---|---|---|
$eventId | string | event identifier |
Return valueβ
none
Overviewβ
Cancel (cancel), used to cancel an event, usually used to cancel asynchronous tasks, supports the cancellation of all events whose running context is defined as an independent fiber. like
delay
,repeat
,onSignal
,defer
, etc.
Basic usageβ
$repeatId = \Co\repeat(function () {
echo 'delay task' .PHP_EOL;
}, 1);
$signalId = \Co\onSignal(SIGINT, function () {
echo 'signal task' .PHP_EOL;
});
// Cancel the signal task after 10 seconds
\Co\delay(fn() => \Co\cancel($signalId), 10);
//Cancel the repeated task after 5 seconds
\Co\delay(fn() => \Co\cancel($repeatId), 5);
$delayId = \Co\delay(function () {
echo 'delay task' .PHP_EOL;
}, 10);
// Cancel the task in advance before it occurs
\Co\cancel($delayId);
Notesβ
You cannot cancel an executed event, you can only cancel an unexecuted event, except repeat/signal, Other events are one-time events and will be automatically destroyed once executed. If there are no special requirements, you do not need to manually cancel the event.