cancelify

0.1.5 • Public • Published

cancelify

NPM Version Build Status

A javascript library for making async operations cancelify

Installation

$ npm install cancelify --save

API

cancelify

cancelify()

Returns a new cancelify:

var cancelify = require('cancelify');
 
var cancelable = cancelify();
callAsyncOperation(arg1, arg2, arg3, cancelable.future());
 
setTimeout(function () {
    cancelable.cancel('Operation timed out');
}, 1000);

cancelify.future()

Returns an 'empty' future (one that will never be canceled).

function asyncOperation(arg1, arg2, arg3, future) {
    future = future || cancelify.future();
 
    // Continue with function knowing there is a cancelable
}

cancelable

cancelable.cancel()

Cancels the current async operations.

var cancelify = require('cancelify');
var cancelable = cancelify(function (err, data) {
    if (err) throw err;
    console.log(data);
});
 
asyncOperation(arg1, arg2, arg3, cancelable.future());
 
cancelable.cancel('canceled');

cancelable.canceled([callback])

If no arguments provide, returns true if the cancelable has been canceled:

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

If canceled with a callback, calls callback when the cancelable is canceled (this is probably currently the most useful of these methods).

function get(url, future) {
    var req = request(url, future);
 
    future.canceled(function (reason) {
        req.abort();
    });
}

cancelable.throwIfCanceled()

Throws the reason if the cancelable has been canceled:

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

cancelable.future()

Returns future functions delegates cancelable:

  • cancel
  • canceled
  • throwIfCanceled

License

Copyright (c) 2014 Tao Yuan. Licensed under the MIT license.

Package Sidebar

Install

npm i cancelify

Weekly Downloads

2

Version

0.1.5

License

none

Last publish

Collaborators

  • bitt