@wealthbar/cancellation-token
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

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

Package Sidebar

Install

npm i @wealthbar/cancellation-token

Weekly Downloads

0

Version

2.0.0

License

MIT

Unpacked Size

204 kB

Total Files

27

Last publish

Collaborators

  • nkode0x00
  • tanglebones
  • chrisnicola