@wealthbar/cancellation-token
v2.0.0
Published
token used to signal to a routine to cancel an asynchronous request
Downloads
2
Readme
cancellationToken
Example
In the rpc
system the caller can provide a cancellationToken
. If the caller decides to cancel their request before it has been handled the rpc
system can reject the pending promise and un-stick the code waiting on the request.
function call(
{
name,
params,
cancellationToken,
timeout,
retries,
}: {
name: string,
params?: object,
cancellationToken?: cancellationToken,
timeout?: number,
retries?: number,
}): Promise<any> {
...
if (cancellationToken) {
cancellationToken.onCancelRequested(() => {
const pendingPromise = pending[id];
if (pendingPromise) {
delete pending[id];
pendingPromise.reject({err: "cancelled", code: 444});
}
});
}
...
}