Example #1
0
var CookyChain = require('cooky-chain');

function echo(msg, delegate){
    process.nextTick(function(){
        delegate(null, msg);
    });
}

CookyChain.try(function(){
    var delegate = this.delegate();
    echo('hello!', delegate);
})
.next(function(across){
    console.log(across[0]); // null
    console.log(across[1]); // hello!
});

Example #2
0
var CookyChain = require('cooky-chain');

var result, retry;
CookyChain.try(function(){
    if (Math.random() * 2 > 1) return this.throw('an exception');
})
.next(function(){ result = true; })
.catch(function(err){ result = false; })
.finally(function(){ console.log(result ? 'good!' : 'bad...'); })
.next(function(){
    if (result) return;
    retry = true;
    console.log('try again!');
    if (Math.random() * 2 > 1) return this.throw('an exception');
})
.next(function(){ result = true; })
.catch(function(err){ result = false; })
.finally(function(){
    if (retry) console.log(result ? 'all ok!' : ':-p');
});


Example #3
0
var CookyChain = require('cooky-chain');

CookyChain.try(function(){
    console.log('outer next 1 start');
    this.enter().next(function(){
        console.log('inner next 1');
        return this.exit();
    })
    .next(function(){
        console.log('inner next 2');
    });
    console.log('outer next 1 end');
})
.next(function(){
    console.log('outer next 2');
});