Fork me on GitHub

Class comb.Promise


Defined in: promise.js.

Class Detail
comb.Promise()
Promise object used for handling a thread

Example 1 :

         var myFunc = function(){
             var promise = new Promise();
             //callback the promise after 10 Secs
             setTimeout(hitch(promise, "callback"), 10000);
             return promise;
         }
         var myFunc2 = function(){
             var promises =[];
             for(var i = 0; i < 10; i++){
                 promises.push(myFunc);
             }
             //create a new promise list with all 10 promises
             return new PromiseList(promises);
         }

         myFunc.then(do something...)
         myFunc.addCallback(do something...)
         myFunc.cain(myfunc).then(do something...)
         myFunc.cain(myfunc).addCallback(do something...)

         myFunc2.then(do something...)
         myFunc2.addCallback(do something...)
         myFunc2.cain(myfunc).then(do something...)
         myFunc2.cain(myfunc).addCallback(do something...)
this.__errorCbs = [];
this.__cbs = [];
        
Method Detail
addCallback(cb)
Add a callback to the callback chain of the promise
Parameters:
{Function} cb
the function to callback when the promise is resolved
if (cb) {
    if (this.__fired && this.__results) {
        cb.apply(this, this.__results);
    } else {
        this.__cbs.push(cb);
    }
}
return this;
        
addErrback(cb)
Add a callback to the errback chain of the promise
Parameters:
{Function} cb
the function to callback when the promise errors
if (cb) {
    if (this.__fired && this.__error) {
        cb.apply(this, this.__error);
    } else {
        this.__errorCbs.push(cb);
    }
}
        
callback(anything)
When called all functions registered as callbacks are called with the passed in results.
Parameters:
anything
variable number of results to pass back to listeners of the promise
if (this.__fired) {
    throw new Error("Already fired!");
}
this.__results = Array.prototype.slice.call(arguments);
this.__resolve();
return this;
        
chain(callback, errback)
Call to chaining of promises
Parameters:
callback
method to call that returns a promise to call after this one completes.
errback
method to call if this promise errors.
var promise = new Promise();
this.addCallback(function(results){
    callback.call(this, results).then(hitch(promise, "callback"), hitch(promise, "errback"));
});
this.addErrback(errback);
return promise;
        
{comb.Promise} chainBoth(callback)
Applies the same function that returns a promise to both the callback and errback.
Parameters:
{Function} callback
function to call. This function must return a Promise
Returns:
{comb.Promise} a promise to continue chaining or to resolve with.
var p = this.chain(callback);
this.addErrback(function(results){
    callback.call(this, results).then(hitch(p, "callback"), hitch(p, "errback"));
});
return p;
        
errback(anything)
When called all functions registered as errbacks are called with the passed in error(s)
Parameters:
anything
variable number of errors to pass back to listeners of the promise
if (this.__fired) {
    throw new Error(i);
}
this.__error = Array.prototype.slice.call(arguments);
this.__resolve();
return this;
        
then(callback, errback)
Call to specify action to take after promise completes or errors
Parameters:
{Function} callback Optional, Default: null
function to call after the promise completes successfully
{Function} errback Optional, Default: null
function to call if the promise errors
this.addCallback(callback);
this.addErrback(errback);
return this;
        

Documentation generated by JsDoc Toolkit 2.4.0 on Tue Jan 31 2012 16:14:12 GMT-0600 (CST)