BDefer

1. Get the BDefer file:

BDefer.js (Right click and "Save link as...")

2. Include the file in your project:

<script type="text/javascript" src="BDefer.js"></script>

3. Look at the Example Code ↓

                   	

    /* use case */

    function task(time, fail) {
        
        ctx = {
            msg : 'You can pass a context'
        };
        
        var de = BDefer(ctx);
        
        setTimeout(function () {
            
            if (fail) {
                
                de.reject('fail');
                
            } else {
                
                de.resolve('Done');
                
            }
            
        }, time);
        
        return de.promise();
    };

    var tasks = [task(50), task(200), task(350, true)];
    
    BDefer.when(tasks).then(function (a, b, c) {
        
        console.log(this, a, b, c);
        
    }).fail(function (a, b, c) {
        
        console.log(this, a, b, c)
        
        console.log(123);
        
    });