Giter VIP home page Giter VIP logo

cancellation's Introduction

cancellation

A method for making async operations cancellable

Installation

component

$ component install ForbesLindesay/cancellation

NPM:

$ npm install cancellation

API

tokenSource()

Returns a new CancellationTokenSource:

var tokenSource = require('cancellation');
var source = tokenSource();
callAsyncOperation(arg1, arg2, arg3, source.token);
setTimeout(function () {
  source.cancel('Operation timed out');
}, 1000);

tokenSource.empty

Returns an 'empty' CancellationToken (one that will never be cancelled).

var tokenSource = require('cancellation');
function asyncOperation(arg1, arg2, arg3, cancellationToken) {
  cancellationToken = cancellationToken || tokenSource.empty;

  //Continue with function knowing there is a cancellation token
}

CancellationToken.isCancelled()

Returns true if the token has been cancelled:

//In ES6
function asyncOperation(cancellationToken) {
  return spawn(function* () {
    while(!cancellationToken.isCancelled()) {
      yield NextAsyncOp();
    }
  })
}

CancellationToken.throwIfCancelled()

Throws the rejection reason if the token has been cancelled:

//In ES6
function asyncOperation(cancellationToken) {
  return spawn(function* () {
    while(true) {
      cancellationToken.throwIfCancelled()
      yield NextAsyncOp();
    }
  })
}

CancellationToken.onCancelled(cb)

Calls cb when the cancellation token is cancelled (this is probably currently the most useful of these methods).

function get(url, cancellationToken) {
  var def = defer();
  
  var req = request(url, function (err, res) {
    if (err) def.reject(err);
    else def.resolve(res);
  });

  cancellationToken
    .onCancelled(function (reason) {
      def.reject(reason);
      req.abort();
    });

  return def.promise;
}

License

MIT

cancellation's People

Contributors

domenic avatar forbeslindesay avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

rubythonode

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.