-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Describe way how to detect cancellation reason #10
Comments
The initial spec text adds a I did not add |
If we decide to take on the Stretch Goals in the proposal then you could possibly leverage a custom async function doRequest(token: CancellationToken): Promise<any> {
const timeoutTokenSource = new CancellationTokenSource();
setTimeout(() => timeoutTokenSource.cancel(new TimeoutError()), 5000);
const requestTokenSource = new CancellationTokenSource([
timeoutTokenSource.token,
token
]);
try {
return await doHttpRequest(requestTokenSource.token);
} catch (err) {
if (err instanceof TimeoutError) {
...
}
throw err;
}
} |
By specification async function doRequest(token: CancellationToken) {
if (token.cancellationRequested) {
throw new CancelError();
}
}
const tokenSource = new CancellationTokenSource();
doRequest(tokenSource.token).catch(err => {
assert(err instanceof CancelError);
assert(err.token === undefined);
});
tokenSource.cancel(); |
In Maybe class CancelError {
/**
* Gets a token associated with the operation that was canceled.
*/
token?: CancellationToken;
constructor(message?: string, token?: CancellationToken);
} |
I'm closing this issue as the proposal no longer specifies cancellation behavior. See #22 for the current thinking regarding cancellation. |
Problem
Assume that we have function which can be cancelled from two sources:
In this case is not possible to distinguish from outside cancellation by timeout and by passed
token
, because if cancellation requested, functiondoRequest()
always returns promise rejected withCancelError
.In this article author suggests approach with checking tokens for cancellation in
catch
block:Questions
Is it correct approach?
Should
CancelaltionTokenSource
class hascancellationRequested
property (like in .NET)? So we can writeinstead
The text was updated successfully, but these errors were encountered: