手写 promise

180 阅读2分钟
class MyPromise{
    constructor(fn){
        try{
            fn(this.resolve , this.reject);
        }catch(error){
            this.reject(error);
        }
    }
    status = 'pending'
    value = null 
    error = null
    onresolve = null
    onReject = null
    onCatch = null
    resolve = value => {
        if(this.status == 'pending'){
            this.status = 'success';
            this.value = value;
            this.onresolve && this.onresolve(value);
        }
    }   
    reject = error => {
        if(this.status == 'pending'){
            this.status = 'fail';
            this.error = error;
            this.onReject && this.onReject(error);
            this.onCatch && this.onCatch(error);
        }
    }
    then = (onresolve , onReject) => new MyPromise((resolve , reject) => {
        if(this.status == 'pending'){
            if(typeof onresolve == 'function'){
                this.onresolve = value => resolve(onresolve(value));
            }else{
                this.onresolve = value => resolve(value);
            }
            if(typeof onReject == 'function'){
                this.onReject = error => resolve(onReject(error));
            }else{
                this.onReject = error => reject(error);
            }
        }
        if(this.status == 'success' ){
            if(typeof onresolve == 'function'){
                resolve(onresolve(this.value));
            }
            else{
                resolve(this.value);
            }
        }
        else if(this.status == 'fail' ){
            if(typeof onReject == 'function'){
                resolve(onReject(this.error));
            }
            else{
                reject(this.error);
            }
        }
    })
    catch = onCatch => new MyPromise((resolve , reject) => {
        if(this.status == 'pending'){
            if(typeof onCatch == 'function'){
                this.onCatch = error => resolve(onCatch(error))
            }else{
                this.onCatch = error => reject(error)
            }
        }
        if(this.status == 'fail'){
            if(typeof onCatch == 'function'){
                resolve(onCatch(error));
            }else{
                reject(error);
            }
        }
    })
    static race = list => new MyPromise((resolve , reject) => {
        list.map(item => {
            if(item instanceof MyPromise){
                item.then(data => resolve(data));
            }
            else{
                MyPromise.resolve(item).then(data => resolve(data));
            }
        })
    })
    static all = list => new MyPromise((resolve , reject) => {
        let dataList = new Array(list.length).fill(null);
        function setData(data , index){
            dataList[index] = data;
            if(dataList.findIndex(item => item !== null) == -1){
                resolve(dataList);
            }
        }
        list.map((item , index) => {
            if(item instanceof MyPromise){
                item.then(data => resolve(data , index));
            }
            else{
                MyPromise.resolve(item).then(data => resolve(data , index));
            }
        })
    })
    static resolve = value => new MyPromise(resolve => resolve(value))
    static reject = error => new MyPromise((_ , reject) => reject(error))
}



function test(caseList){
    // new promise
    if(caseList.indexOf(1) > -1){
        new Promise(resolve => resolve(1)).then(data => console.log(data));
        new Promise((resolve , reject) => reject('错误')).then(null , error => console.log(error));

        new MyPromise(resolve => resolve(1)).then(data => console.log(data));
        new MyPromise((resolve , reject) => reject('错误')).then(null , error => console.log(error));
    }
    // 异步
    if(caseList.indexOf(2) > -1){
        new Promise(resolve => setTimeout(() => resolve(2))).then(data => console.log(data));
        new MyPromise(resolve => setTimeout(() => resolve(2))).then(data => console.log(data));
    }
    // 链式调用
    if(caseList.indexOf(3) > -1){
        new Promise(resolve => setTimeout(() => resolve(3))).then(data => data * 2).then(data => console.log(data));
        new MyPromise(resolve => setTimeout(() => resolve(3))).then(data => data * 2).then(data => console.log(data));
    }

    // static resolve reject
    if(caseList.indexOf(4) > -1){
        Promise.resolve(4).then(data => console.log(data));
        MyPromise.resolve(4).then(data => console.log(data))
    }
    // 穿透
    if(caseList.indexOf(5) > -1){
        Promise.resolve(5).then().then(data => console.log(data));
        MyPromise.resolve(5).then().then(data => console.log(data))
    }
    // catch
    if(caseList.indexOf(6) > -1){
        new Promise((resolve , reject) => setTimeout(() => reject('错误6'))).catch(error => console.log(error));
        new MyPromise((resolve , reject) => setTimeout(() => reject('错误6'))).catch(error => console.log(error));
    }
    // race
    if(caseList.indexOf(7) > -1){
        let list = [1,2,3].map(item => new Promise(resolve => setTimeout(() => {
            console.log(`promise 任务${item}完成`);
            resolve(item);
        } , Math.random() * 3000)));
        Promise.race(list).then(item => console.log(`promise任务${item}打印`));

        
    }
    if(caseList.indexOf(8) > -1){
        let list = [1,2,3].map(item => new MyPromise(resolve => setTimeout(() => {
            console.log(`mypromise 任务${item}完成`);
            resolve(item);
        } , Math.random() * 3000)));
        MyPromise.race(list).then(item => console.log(`mypromise任务${item}打印`))
    }
    // all
    if(caseList.indexOf(9) > -1){
        let list = [1,2,3].map(item => new Promise(resolve => setTimeout(() => {
            console.log(`promise 任务${item}完成`);
            resolve(item);
        } , Math.random() * 3000)));
        Promise.all(list).then(item => console.log(`promise任务${item}打印`));

        
    }
    if(caseList.indexOf(10) > -1){
        let list = [1,2,3].map(item => new MyPromise(resolve => setTimeout(() => {
            console.log(`mypromise 任务${item}完成`);
            resolve(item);
        } , Math.random() * 3000)));
        MyPromise.all(list).then(item => console.log(`mypromise任务${item}打印`))
    }
}


test([10])



插播一条广告 公司招聘

971623142088_.pic.jpg