await-generator icon indicating copy to clipboard operation
await-generator copied to clipboard

Cancellable interface

Open SOF3 opened this issue 4 years ago • 0 comments

I don't know if "cancel" is a good term; I am not even sure if cancellation is a correct thing to do in a promise framework, where promise is just a state machine (unlike Rust futures, where you need to await a future to actually execute it).

Design

function read_line_await(LineBufReader $reader) {
    $resolve = yield Await::RESOLVE;
    $isCancelled = yield Await::IS_CANCEL;
    $reader->shiftLineCallback(function($line) use($isCancelled, $resolve) {
        if($isCancelled()) {
            $reader->unshiftLine($line);
        } else {
            $resolve($line);
        }
    });
}

When we Await::race multiple generators, the losing generators are set to be cancelled

Concerns

Coroutines are not necessarily cancellatoin-aware. They need to explicitly indicate what they do when they are cancelled (in particular, stateful promises need to reset the states they changed), which is a usability issue.

SOF3 avatar Nov 24 '21 15:11 SOF3