0.1.5 • Published 12 years ago

bond v0.1.5

Weekly downloads
21
License
-
Repository
github
Last release
12 years ago

Bond - A Simple CommonJS Promises Implementation

Bond is a simple implementation of the Promises/A specification.

It allows authors to write promise-compatible methods; which in turn allow users to add multiple functions to call on "success" or "failure" of the original call.

Example

// This is a promise-compatible method that
// waits for 10 milliseconds and then keep
// or break the promise depending on the
// 'pass' argument.
var wait10millis = function(pass) {
    var dfd = bond.deferred();
    setTimeout(function() {
        if ( pass ) {
            dfd.resolve();
        } else {
            dfd.reject();
        }
    }, 10);
    // promise methods must return a promise
    return dfd.promise();
};


// USAGE EXAMPLES:

// single "then()" (or 'success')
wait10millis(true).then(function() {
    alert('yay');
});

// single "then()", single "fail()":
wait10millis( (Math.rand()>0.5) )
    .then(function() { alert('yay'); })
    .fail(function() { alert('boo'); });

// keep the promise to add more methods later
var promise = wait10millis( (Math.rand()>0.5) );
// NB - this will all happen long after the 10 millis
setTimeout(function() {
    promise.then(function() { alert('hi'); });
    promise.then(function() { alert('ho'); });
    promise.fail(function() { alert('no'); });
    promise.fail(function() { alert('go'); });
}, 100);
0.1.5

12 years ago

0.1.4

12 years ago

0.1.3

12 years ago

0.1.2

12 years ago

0.1.1

12 years ago

0.1.0

12 years ago