// executor  function(resolve, reject) { }   );
const PENDING = 'pending';
const RESOLVED = 'resolved';
const REJECTED = 'rejected';

function MyPromise(executor) {
    status = PENDING;
    data = null;
    callbacks = []; //{onResolve,onReject}

    function resolve(val) {
        debugger;
        this.status = RESOLVED;
        this.data = val;
        if (this.callbacks.length > 0) {
            this.callbacks[0](val);
        }
    }

    function reject(reason) {}

    try {
        executor(resolve, reject);
    } catch (error) {}
    executor(resolve, reject);
}

MyPromise.prototype.then = (onResolve, onReject) => {
    this.callbacks.push({
        onResolve,
        onReject
    });
    return new MyPromise((res, rej) => {
        if (this.status === RESOLVED) {
            onResolve(this.data);
        } else if (this.status === RESOLVED) {
            onReject(this.data);
        } else {

        }
    });
}


MyPromise.prototype.catch = function (onRejected) {};

MyPromise.prototype.resolve = function (val) {};

MyPromise.prototype.reject = function (reason) {};

var p = new MyPromise(function (res, rej) {
    console.log('init');
    setTimeout(() => {
        debugger;
        res('a');
    }, 1000);
});
p.then(
    val1 => {
        console.log(val1);
    },
    reason1 => {

    }
).then(
    val2 => {
        console.log(val2);
    },
    reason2 => {

    }
);

// 1: init MyPromise,  put a to Quee
// 2:

/**
 * output     Micro Quee      Mis Quee
 * init       a
 *
 *
 */

posted on 2020-09-03 17:55  码农-编程小子  阅读(137)  评论(0编辑  收藏  举报