src/deferred.js

Method deferred

deferred factory

Returns an Object
(Deferred instance)

var deferred = function () { return new Deferred(); };

Constructor

Deferred

function Deferred () { var self = this; this.promise = promise.factory(); this.onDone = []; this.onAlways = []; this.onFail = [];

Setting handlers to execute Arrays of Functions

this.promise.then( function ( arg ) { promise.delay( function () { array.each( self.onDone, function ( i ) { i( arg ); }); array.each( self.onAlways, function ( i ) { i( arg ); }); self.onAlways = []; self.onDone = []; self.onFail = []; }); }, function ( arg ) { promise.delay( function () { array.each( self.onFail, function ( i ) { i( arg ); }); array.each( self.onAlways, function ( i ) { i( arg ); }); self.onAlways = []; self.onDone = []; self.onFail = []; }); }); }

Setting constructor loop

Deferred.prototype.constructor = Deferred;

Method always

Registers a function to execute after Promise is reconciled

Parameters:

  • arg must be a Function.
    (Function to execute)

Returns an Object
(Deferred instance)

Deferred.prototype.always = function ( arg ) { if ( typeof arg !== "function" ) { throw new Error( label.error.invalidArguments ); } else if ( this.promise.state > 0 ) { throw new Error( label.error.promiseResolved.replace( "{{outcome}}", this.promise.value ) ); } this.onAlways.push( arg ); return this; };

Method done

Registers a function to execute after Promise is resolved

Parameters:

  • arg must be a Function.
    (Function to execute)

Returns an Object
(Deferred instance)

Deferred.prototype.done = function ( arg ) { if ( typeof arg !== "function" ) { throw new Error( label.error.invalidArguments ); } else if ( this.promise.state > 0 ) { throw new Error( label.error.promiseResolved.replace( "{{outcome}}", this.promise.value ) ); } this.onDone.push( arg ); return this; };

Method fail

Registers a function to execute after Promise is rejected

Parameters:

  • arg must be a Function.
    (Function to execute)

Returns an Object
(Deferred instance)

Deferred.prototype.fail = function ( arg ) { if ( typeof arg !== "function" ) { throw new Error( label.error.invalidArguments ); } else if ( this.promise.state > 0 ) { throw new Error( label.error.promiseResolved.replace( "{{outcome}}", this.promise.value ) ); } this.onFail.push( arg ); return this; };

Method isRejected

Determines if Deferred is rejected

Returns a Boolean
(true if rejected)

Deferred.prototype.isRejected = function () { return ( this.promise.state === promise.state.FAILED ); };

Method isResolved

Determines if Deferred is resolved

Returns a Boolean
(true if resolved)

Deferred.prototype.isResolved = function () { return ( this.promise.state === promise.state.SUCCESS ); };

Method reject

Rejects the Promise

Parameters:

  • arg can be of any type.
    (Rejection outcome)

Returns an Object
(Deferred instance)

Deferred.prototype.reject = function ( arg ) { this.promise.reject.call( this.promise, arg ); return this; };

Method resolve

Resolves the Promise

Parameters:

  • arg can be of any type.
    (Resolution outcome)

Returns an Object
(Deferred instance)

Deferred.prototype.resolve = function ( arg ) { this.promise.resolve.call( this.promise, arg ); return this; };

Method state

Gets the state of the Promise

Returns a String
(Describes the state)

Deferred.prototype.state = function () { return this.promise.state; };

Method then

Registers handler(s) for the Promise

Parameters:

  • success must be a Function.
    (Executed when/if promise is resolved)

  • failure must be a Function.
    ([Optional] Executed when/if promise is broken)

Returns an Object
(New Promise instance)

Deferred.prototype.then = function ( success, failure ) { return this.promise.then( success, failure ); };