async
async copied to clipboard
delay() should throw a custom exception, implement throwIfCanceled()
Hello,
I believe it would be more appropriate for delay() to throw a custom exception specific to cancellation (e.g., CanceledException) instead of a standard PHP's \RuntimeException.
This would allow developers to more easily distinguish between expected cancellations and other unexpected errors.
Additionally, it would be great if ReactPHP could provide a helper function, like this one:
function throwIfCanceled() {
await(new Promise(
function($resolve) {
$resolve(null);
},
function() {
throw new CanceledException();
}
));
}
This would allow developers to handle the cancellation of asynchronous tasks at specific points in the code, such as after each iteration of a long-running loop (essentially the same as delay(0)).