Skip to content

promise

js
/*1、Promise 就是一个类 在执行这个类的时候 需要传递一个执行器进去 执行器回立即执行
2、Promise 中有三种状态 分别为 成功 fulfilled 失败 rejected 等待 pending
    penging -> fulfilled
    penging -> rejected
        一旦状态确定就不可更改
3、resolve和reject函数是用来更改状态的
    resolve: fulfilled
    reject: rejected
4、then方法内部做的事情就是判断状态 如果状态是成功 调用成功的回调函数 如果状态是失败 调用失败回调函数
5、then成功回调有一个参数 表示成功之后的值 then失败回调有一个参数 表示失败后的原因
*/
let promise = new Promise((resolve, reject) => {
    resolve('成功')
    reject('失败')
})

promise.then(() => {}, () => {})


const PENDING = 'pending'
const FULFILLED = 'fulfilled'
const REJECTED = 'rejected'

class MyPromise {
    constructor (executor) {
        executor(this.resolve, this.reject)
    }
    status = PENDING;
    value = undefined;
    reason = undefined;
    successCallback = [];
    failCallback = [];
    resolve = value => {
        // 如果状态不是等待 阻止程序向下执行
         if(this.status !== PENDING) return;
        // 将状态更改为成功
        this.status = FULFILLED;
        this.value = value;
        // 判断成功回调是否存在 如果存在 调用
        // this.successCallback && this.successCallback(this.value);
        while(this.successCallback.length){
            this.successCallback.shift()(this.value);
        }
    }
    reject = reason => {
        // 如果状态不是等待 阻止程序向下执行
        if(this.status !== PENDING) return;
        // 将状态更改为失败
        this.status = REJECTED;
        this.reason = reason;
        // this.failCallback && this.failCallback(this.value);
        while(this.failCallback.length){
            this.failCallback.shift()(this.reason);
        }
    }
    then(successCallback, failCallback) {
        let primise2 = new MyPromise((resolve, reject) => {
            // 判断状态
            if(this.status === FULFILLED){
                let x = successCallback(this.value);
                // 判断x的值是普通值还是promise对象
                // 如果是普通值 直接调用resolve
                // 如果是promise对象 查看 promise对象返回的结果
                // 在根据promise对象返回的结果 决定调用resolve 还是调用 reject
                resolve(x);
            }else if(this.status === REJECTED){
                failCallback(this.reason)
            }else{
                // 等待
                // 将成功回调 失败回调 存储起来
                this.successCallback.push(successCallback);
                this.failCallback.push(failCallback);
            }
        });
        
        return promise2
    }
}

function resolvePromise (x, resolve, reject){
    if(x instanceof MyPromise){
        // x promise对象
        x.then(resolve, reject)
    }else{
        // x 普通值
        resolve(x);
    }
}
/*1、Promise 就是一个类 在执行这个类的时候 需要传递一个执行器进去 执行器回立即执行
2、Promise 中有三种状态 分别为 成功 fulfilled 失败 rejected 等待 pending
    penging -> fulfilled
    penging -> rejected
        一旦状态确定就不可更改
3、resolve和reject函数是用来更改状态的
    resolve: fulfilled
    reject: rejected
4、then方法内部做的事情就是判断状态 如果状态是成功 调用成功的回调函数 如果状态是失败 调用失败回调函数
5、then成功回调有一个参数 表示成功之后的值 then失败回调有一个参数 表示失败后的原因
*/
let promise = new Promise((resolve, reject) => {
    resolve('成功')
    reject('失败')
})

promise.then(() => {}, () => {})


const PENDING = 'pending'
const FULFILLED = 'fulfilled'
const REJECTED = 'rejected'

class MyPromise {
    constructor (executor) {
        executor(this.resolve, this.reject)
    }
    status = PENDING;
    value = undefined;
    reason = undefined;
    successCallback = [];
    failCallback = [];
    resolve = value => {
        // 如果状态不是等待 阻止程序向下执行
         if(this.status !== PENDING) return;
        // 将状态更改为成功
        this.status = FULFILLED;
        this.value = value;
        // 判断成功回调是否存在 如果存在 调用
        // this.successCallback && this.successCallback(this.value);
        while(this.successCallback.length){
            this.successCallback.shift()(this.value);
        }
    }
    reject = reason => {
        // 如果状态不是等待 阻止程序向下执行
        if(this.status !== PENDING) return;
        // 将状态更改为失败
        this.status = REJECTED;
        this.reason = reason;
        // this.failCallback && this.failCallback(this.value);
        while(this.failCallback.length){
            this.failCallback.shift()(this.reason);
        }
    }
    then(successCallback, failCallback) {
        let primise2 = new MyPromise((resolve, reject) => {
            // 判断状态
            if(this.status === FULFILLED){
                let x = successCallback(this.value);
                // 判断x的值是普通值还是promise对象
                // 如果是普通值 直接调用resolve
                // 如果是promise对象 查看 promise对象返回的结果
                // 在根据promise对象返回的结果 决定调用resolve 还是调用 reject
                resolve(x);
            }else if(this.status === REJECTED){
                failCallback(this.reason)
            }else{
                // 等待
                // 将成功回调 失败回调 存储起来
                this.successCallback.push(successCallback);
                this.failCallback.push(failCallback);
            }
        });
        
        return promise2
    }
}

function resolvePromise (x, resolve, reject){
    if(x instanceof MyPromise){
        // x promise对象
        x.then(resolve, reject)
    }else{
        // x 普通值
        resolve(x);
    }
}