Socket
Book a DemoInstallSign in
Socket

cancellation-token

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cancellation-token

JS Cancellation Tokens for composable async cleanup

1.0.0
latest
Source
npmnpm
Version published
Weekly downloads
2
100%
Maintainers
1
Weekly downloads
 
Created
Source

Cancellation Token

Tokens are extremely lightweight and easy to grok, their power lies in their composability. The entire implementation is this:

export default class Token {
  constructor(parent) {
    this._parent = parent;
    this._cancelled = false;
  }

  get cancelled() {
    return this._cancelled || (this._cancelled = this._parent ? this._parent.cancelled : false);
  }

  cancel() {
    this._cancelled = true;
  }
}

Usage

Each example builds on the previous in order.

create

import Token from 'cancellation-token';

let token = new Token();

conditional work

let jobs = [];

function cancellableJob(job, token) {
  return function() {
    if (!token.cancelled) {
      job();
    }
  };
}

function schedule(job) {
  let token = new Token();
  
  jobs.push(cancellableJob(job, token);
  
  return token;
}

cancelling work

let scheduledJob = schedule(() => { alert("hello world!"); });
scheduledJob.cancel();

inheritance

let grandParentToken = new Token();
let parentToken = new Token(grandParentToken);

function scheduleWithParent(job, parentToken) {
  let token = new Token(parentToken);
  
  jobs.push(cancellableJob(job, token);
  
  return token;
}

// cancel the task and all descendants
grandParentToken.cancel();

Keywords

cancellation

FAQs

Package last updated on 28 Oct 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.