discard groups of callbacks
Latest commit b1c5f16 Jan 14, 2012 @substack package.json, better desc



Abort groups of callbacks.

This is a handy module for cancelling nested asynchronous logic when some external event comes along like a timeout that should abort an entire transaction.

Toss is like domains in node but implemented as a function wrapper instead of a something in core.


var toss = require('toss');
var fs = require('fs');

var t = toss(10, function (err) {
    console.log('reads took to long');

fs.readFile(__filename, t(function (err, body) {
    if (err) t.error(err)
    else fs.readFile(__filename + '/no_such_file', t(function (err, body) {
        if (err) t.error(err)
        else t.end()


var toss = require('toss')

var t = toss(timeout=0, cb)

Create a toss function t with an optional timeout and an optional error callback cb.

cb fires when a callback is attempted after t.end() is called or when t.error() is explicitly called.

If timeout is specified and t.end() hasn't been called in timeout milliseconds, t.end() will be called automatically.

t(timeout=0, cb)

Returns a function that calls cb with the arguments provided unless t.end() has been called, in which case the callback from toss() is called.

Put this function around the callbacks that you want to group together.


After this function is called, all calls to functions wrapped with t() will be ignored and the callback from toss() will fire.


Executes t.end() then triggers the callback from toss() with the err object.


With npm do:

npm install toss