bergus / promise-cancellation

Promise cancellation proposal for JavaScript
Other
24 stars 1 forks source link

What should happen to exceptions in cancellation handlers? #9

Open bergus opened 8 years ago

bergus commented 8 years ago
const {token, canel} = CancelToken.source();
const promise = …(token); // some promise that has the token associated
const a = promise.trifurcate(…, …, r => { throw new Error(); });
const b = promise.finally(() => { throw new Error(); });
const c = token.subscribe(r => { throw new Error(); });
const d = token.subscribeOrCall(r => { throw new Error(); }, …);

const res = cancel();

These exceptions are typically unwanted, so we definitely want them reported if no one handles them. All of the above calls do produce a promise that is indeed rejected when the respective handler throws:

So there's always a promise that is getting rejected and could be tracked. However, in the case A the promise is typically part of some chain, and it is possible if not quite likely that they have a cancelled rejection handler attached to them that prevents tracking (see #5). In the case B, attaching handlers to b doesn't work as it is cancelled. The internal promise is not accessible but does have a cancelled handler attached to it - #5 again. In the case C, the promise is surfaced but usually doesn't get any handlers attached. In the case D, the promise is absolutely not accessible.

Handling these rejections explicitly is only possible in the cases A and C.

The current proposal presents the idea that all these rejected promises are returned by the cancel() call in an array to the canceller: res=[a, internalB, c, internalD]. The canceller (i.e. the actor that requests the cancellation) is thereby enabled to handle exceptions that happened during the cancellation. It also receives normal return values from the cancellation handlers.

Does this make sense? Or is this absolutely insane? Are there any problems with leaking information? Is this useful at all?

If yes, should there be a special way to attach a handler to a token without returning such a result? And what about composition of tokens, where handlers would return a promise for an array of handler results?