🚨 Shai-Hulud Strikes Again:834 Packages Compromised.Technical Analysis →
Socket
Book a DemoInstallSign in
Socket

canc-all

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

canc-all

Create a cancellable wrapper for an arbitrary tree of async tasks.

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

cancAll

Create a cancellable wrapper for an arbitrary tree of async tasks.

Usage

Read the tests for a thorough understanding.

Basic usage:

// Import cancAll
var cancAll = require('cancAll');


// Create a cancellable task wrapper
var task = cancAll();


// Wrap required functions in cancellability
var someTask = task(function(callback){

    // Respond after 1 second.
    setTimeout(function(){
        callback(null, 'result');
    }, 1000);
});

var someDependantTask = task(function(callback){

    // call `someTask` and passthrough results.
    someTask(callback);
});

// use your wrapped function

someDependantTask(function(error, result){

    // Error from cancellation below.
    error.message === 'Reason for cancellation'
});

// Cancel the task while in flight

task.cancel(new Error('Reason for cancellation'));

More useful, deep task graph usage:

var cancAll = require('cancAll');


// Define some nicely decoupled tasks.
function getNumber(callback){
    setTimeout(function(){
        callback(null, 1);
    });
}

function getNumber2(callback){
    setTimeout(function(){
        callback(null, 2);
    });
}

function slowAddValues(a, b, callback){

    // This task does get called, but it takes too long,
    // and results are ignored

    setTimeout(function(){
        callback(null, a + b);
    }, 100);
}

function passThroughNeverCalled(result, callback){

    // This task never gets called due to cancellation.

    setTimeout(function(){
        callback(null, result);
    });
}


// Build our async graph
var number = righto(task(getNumber));
var number2 = righto(task(getNumber2));
var added = righto(task(slowAddValues), number, number2);
var result = righto(task(passThroughNeverCalled), added);

// Execute the graph
result(function(error, result){

    // Got expected cancellation
    console.log(error.message) // 'timeout';
});

// Cancel execution part-way through
setTimeout(function(){
    task.cancel(new Error('timeout'));
}, 50);

FAQs

Package last updated on 08 Jan 2019

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