2.0.0 • Published 5 years ago

@wealthbar/cancellation-token v2.0.0

Weekly downloads
1
License
MIT
Repository
gitlab
Last release
5 years ago

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