function Promise(resolver) {
     if (!(this instanceof Promise)) {
        return new Promise(resolver);
    }
    var queue = [];
    var resolved = false;
    // The `handler` variable points to the function that will
    // 1) handle a .then(onFulfilled, onRejected) call
    // 2) handle a .resolve or .reject call (if not fulfilled)
    // Before 2), `handler` holds a queue of callbacks.
    // After 2), `handler` is a simple .then handler.
    // We use only one function to save memory and complexity.
     // Case 1) handle a .then(onFulfilled, onRejected) call
    function pending(onFulfilled, onRejected){
        return Promise(function(resolver,rejecter){
            queue.push({
                resolve: onFulfilled,
                reject: onRejected,
                resolver:resolver,
                rejecter:rejecter
            });
        });
    }
    function then(onFulfilled, onRejected) {
        return resolved?resolved(onFulfilled, onRejected):pending(onFulfilled, onRejected);
    }
    // Case 2) handle a .resolve or .reject call
        // (`onFulfilled` acts as a sentinel)
        // The actual function signature is
        // .re[ject|solve](sentinel, success, value)
    function resolve(success, value){
        var action = success ? 'resolve' : 'reject';
        var queued;
        var callback;
        for (var i = 0, l = queue.length; i < l; i++) {
            queued = queue[i];
            callback = queued[action];
            if (typeof callback === 'function') {
                immediate(execute,callback, value, queued.resolver, queued.rejecter);
            }else if(success){
                queued.resolver(value);
            }else{
                queued.rejecter(value);
            }
        }
        // Replace this handler with a simple resolved or rejected handler
        resolved = createHandler(then, value, success);
    }
    this.then = then;
    function yes(value) {
        if (!resolved) {
            resolve(true, value);
        }
    }
    function no (reason) {
        if (!resolved) {
            resolve(false, reason);
        }
    }
    try{
        resolver(function(a){
            if(a && typeof a.then==='function'){
                a.then(yes,no);
            }else{
                yes(a);
            }
        },no);
    }catch(e){
        no(e);
    }
}
// Creates a deferred: an object with a promise and corresponding resolve/reject methods
Creates a deferred: an object with a promise and corresponding resolve/reject methods
Creates a deferred : an object with a promise and corresponding resolve / reject methods
function Promise(resolver) {
     if (!(this instanceof Promise)) {
        return new Promise(resolver);
    }
    var queue = [];
    var resolved = false;
    function pending(onFulfilled, onRejected){
        return Promise(function(resolver,rejecter){
            queue.push({
                resolve: onFulfilled,
                reject: onRejected,
                resolver:resolver,
                rejecter:rejecter
            });
        });
    }
    function then(onFulfilled, onRejected) {
        return resolved?resolved(onFulfilled, onRejected):pending(onFulfilled, onRejected);
    }
    function resolve(success, value){
        var action = success ? 'resolve' : 'reject';
        var queued;
        var callback;
        for (var i = 0, l = queue.length; i < l; i++) {
            queued = queue[i];
            callback = queued[action];
            if (typeof callback === 'function') {
                immediate(execute,callback, value, queued.resolver, queued.rejecter);
            }else if(success){
                queued.resolver(value);
            }else{
                queued.rejecter(value);
            }
        }
        resolved = createHandler(then, value, success);
    }
    this.then = then;
    function yes(value) {
        if (!resolved) {
            resolve(true, value);
        }
    }
    function no (reason) {
        if (!resolved) {
            resolve(false, reason);
        }
    }
    try{
        resolver(function(a){
            if(a && typeof a.then==='function'){
                a.then(yes,no);
            }else{
                yes(a);
            }
        },no);
    }catch(e){
        no(e);
    }
}
function Promise ( resolver ) { if ( ! ( this instanceof Promise ) ) { return new Promise ( resolver ) ; } var queue = [ ] ; var resolved = false ; function pending ( onFulfilled , onRejected ) { return Promise ( function ( resolver , rejecter ) { queue . push ( { resolve : onFulfilled , reject : onRejected , resolver : resolver , rejecter : rejecter } ) ; } ) ; } function then ( onFulfilled , onRejected ) { return resolved ? resolved ( onFulfilled , onRejected ) : pending ( onFulfilled , onRejected ) ; } function resolve ( success , value ) { var action = success ? 'resolve' : 'reject' ; var queued ; var callback ; for ( var i = 0 , l = queue . length ; i < l ; i ++ ) { queued = queue [ i ] ; callback = queued [ action ] ; if ( typeof callback === 'function' ) { immediate ( execute , callback , value , queued . resolver , queued . rejecter ) ; } else if ( success ) { queued . resolver ( value ) ; } else { queued . rejecter ( value ) ; } } resolved = createHandler ( then , value , success ) ; } this . then = then ; function yes ( value ) { if ( ! resolved ) { resolve ( true , value ) ; } } function no ( reason ) { if ( ! resolved ) { resolve ( false , reason ) ; } } try { resolver ( function ( a ) { if ( a && typeof a . then === 'function' ) { a . then ( yes , no ) ; } else { yes ( a ) ; } } , no ) ; } catch ( e ) { no ( e ) ; } }
Creates a deferred: an object with a promise and corresponding resolve/reject methods
Creates a deferred : an object with a promise and corresponding resolve / reject methods
// The `handler` variable points to the function that will // 1) handle a .then(onFulfilled, onRejected) call // 2) handle a .resolve or .reject call (if not fulfilled) // Before 2), `handler` holds a queue of callbacks. // After 2), `handler` is a simple .then handler. // We use only one function to save memory and complexity. // Case 1) handle a .then(onFulfilled, onRejected) call // Case 2) handle a .resolve or .reject call // (`onFulfilled` acts as a sentinel) // The actual function signature is // .re[ject|solve](sentinel, success, value) // Replace this handler with a simple resolved or rejected handler