async icon indicating copy to clipboard operation
async copied to clipboard

delay() should throw a custom exception, implement throwIfCanceled()

Open appkit-framework opened this issue 1 month ago • 0 comments

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)).

appkit-framework avatar Dec 06 '25 02:12 appkit-framework