src/promise.js

Method delay

Returns a Function
(Delay method)

delay : function () { if ( typeof setImmediate !== "undefined" ) { return function ( arg ) { setImmediate( arg ); }; } else if ( typeof process !== "undefined" ) { return process.nextTick; } else { return function ( arg ) { setTimeout( arg, 0 ); }; } }(),

Method factory

Promise factory

Returns an Object
(Instance of promise)

factory : function () { return new Promise(); },

Method pipe

Pipes a reconciliation from parent to child

Parameters:

  • parent must be an Object.
    (Promise)

  • child must be an Object.
    (Promise)

Returns an Undefined
(undefined)

pipe : function ( parent, child ) { parent.then( function ( arg ) { child.resolve( arg ); }, function ( e ) { child.reject( e ); }); },

Initiates processing a Promise

Parameters:

  • obj must be an Object.
    (Promise instance)

  • arg can be of any type.
    (Promise value)

  • state must be a Number.
    (State, e.g. "1")

Returns an Object
(Promise instance)

process : function ( obj, arg, state ) { if ( obj.state > promise.state.PENDING ) { return; } obj.value = arg; obj.state = state; if ( !obj.deferred ) { promise.delay( function () { obj.process(); }); obj.deferred = true; } return obj; },

States of a Promise

state : { PENDING : 0, FAILURE : 1, SUCCESS : 2 } };

Constructor method Promise

Promise

Returns an Object
(Promise instance)

function Promise () { this.deferred = false; this.handlers = []; this.state = promise.state.PENDING; this.value = null; }

Setting constructor loop

Promise.prototype.constructor = Promise;

Method process

Processes handlers queue

Returns an Object
(Promise instance)

Promise.prototype.process = function() { var result, success, value; this.deferred = false; if ( this.state === promise.state.PENDING ) { return; } value = this.value; success = this.state === promise.state.SUCCESS; array.each( this.handlers.slice(), function ( i ) { var callback = i[success ? "success" : "failure" ], child = i.promise; if ( !callback || typeof callback !== "function" ) { if ( value && typeof value.then === "function" ) { promise.pipe( value, child ); } else { if ( success ) { child.resolve( value ); } else { child.reject( value ); } } return; } try { result = callback( value ); } catch ( e ) { child.reject( e ); return; } if ( result && typeof result.then === "function" ) { promise.pipe( result, promise ); } else { child.resolve( result ); } }); return this; };

Method reject

Breaks a Promise

Parameters:

  • arg can be of any type.
    (Promise value)

Returns an Object
(Promise instance)

Promise.prototype.reject = function ( arg ) { return promise.process( this, arg, promise.state.FAILURE ); };

Method resolve

Resolves a Promise

Parameters:

  • arg can be of any type.
    (Promise value)

Returns an Object
(Promise instance)

Promise.prototype.resolve = function ( arg ) { return promise.process( this, arg, promise.state.SUCCESS ); };

Method then

Registers handler(s) for a Promise

Parameters:

  • success must be a Function.
    ([Optional] Success handler for eventual value)

  • failure must be a Function.
    ([Optional] Failure handler for eventual value)

Returns an Object
(New Promise instance)

Promise.prototype.then = function ( success, failure ) { var self = this, child = new Promise(); this.handlers.push( { success : success, failure : failure, promise : child } ); if ( this.state > promise.state.PENDING && !this.deferred ) { promise.delay( function () { self.process(); }); this.deferred = true; } return child; };