当前位置: 首页 > article >正文

手写Promise

构造器的实现

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

class MyPromise{

    #state = PENDING;
    #result = undefined;

    constructor(executor){
        const resolve = (data) => {
            this.#changeState(FULFILLED, data);
        };
        const reject = (reason) => {
            this.#changeState(REJECTED, reason);
        };
        try {
            executor(resolve, reject);
        } catch (err) {
            reject(err);
        }        
    }

    #changeState(state, result){
        if(this.#state !== PENDING) return;
        this.#state = state;
        this.#result = result;
    }
}
const p = new MyPromise((resolve, reject) =>{
	resolve(1);
});
  • Promise 的状态一旦定下来之后,在后续的所有过程中都不可能再去改变它的状态了
  • 如果再执行过程中报错,会执行rejected,但是捕获不到异步错误

Promise-then的回调执行时机

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

class MyPromise{

    #state = PENDING;
    #result = undefined;
    #handlers = [];

    constructor(executor){
        const resolve = (data) => {
            this.#changeState(FULFILLED, data);
        };
        const reject = (reason) => {
            this.#changeState(REJECTED, reason);
        };
        try {
            executor(resolve, reject);
        } catch (err) {
            reject(err);
        }        
    }

    #changeState(state, result){
        if(this.#state !== PENDING) return;
        this.#state = state;
        this.#result = result;
        this.#run();
    }
    
    #run(){
        if(this.#state === PENDING) return;
        while(this.#handlers.length){
            const {onFulfilled, onRejected, resolve, reject} = this.#handlers.shift();
            if(this.#state === FULFILLED){
                if(typeof onFulfilled === 'function'){
                    onFulfilled(this.#result)
                }
            }
            else{
                if(typeof onRejected === 'function'){
                    onRejected(this.#result)
                }
            }
        }
    }

    then(onFulfilled, onRejected){
        return new MyPromise((resolve, reject) =>{
            this.#handlers.push({
                onFulfilled,
                onRejected,
                resolve,
                reject,
            });
            this.#run();
        });
    }
}

const p = new MyPromise((resolve, reject) =>{
	setTimeout(()=>{
        resolve(123);
    },1000)
});

p.then(
    (res) =>{
        console.log('promise 成功1', res);
    }, 
    (err) =>{
        console.log('promise 失败1', err);
    }
);
p.then(
    (res) =>{
        console.log('promise 成功2', res);
    }, 
    (err) =>{
        console.log('promise 失败2', err);
    }
);
p.then(
    (res) =>{
        console.log('promise 成功3', res);
    }, 
    (err) =>{
        console.log('promise 失败3', err);
    }
);

为了判断什么时候调用,所以用一个handlers数组记录,并用run函数调用

Promise-then 的返回值

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

class MyPromise{

    #state = PENDING;
    #result = undefined;
    #handlers = [];

    constructor(executor){
        const resolve = (data) => {
            this.#changeState(FULFILLED, data);
        };
        const reject = (reason) => {
            this.#changeState(REJECTED, reason);
        };
        try {
            executor(resolve, reject);
        } catch (err) {
            reject(err);
        }        
    }

    #changeState(state, result){
        if(this.#state !== PENDING) return;
        this.#state = state;
        this.#result = result;
        this.#run();
    }

    // 判断一个函数是不是Promise(是否满足A+规范)
    #isPromiseLike(value){
        return value && typeof value.then === 'function' && (typeof value === 'object' || typeof value === 'function');
    }

    // 把一个函数放到微队列
    #runMicroTask(func){
        // node nextTick
        if(process && process.nextTick){
            process.nextTick(func);
        }
        // 浏览器 MutationObserver
        else if(typeof MutationObserver === 'function'){
            const ob = new MutationObserver(fn);
            const text = document.createTextNode('1');
            ob.observe(text, { characterData: true});
            text.data = '2';
        }
        else{
            setTimeout(fn, 0);
        }
    }

    #runOne(callback, resolve, reject){
        this.#runMicroTask(()=>{
            if(typeof callback !== 'function'){
                const settled = this.#state === FULFILLED ? resolve : reject;
                settled(this.#result);
                return;
            }
            try {
                const data = callback(this.#result);
                if(this.#isPromiseLike(data)){
                    data.then(resolve, reject);
                }else{
                    resolve(data);
                }
            } catch (err) {
                reject(err);
            }
        });
    }
    
    #run(){
        if(this.#state === PENDING) return;
        while(this.#handlers.length){
            const {onFulfilled, onRejected, resolve, reject} = this.#handlers.shift();
            if(this.#state === FULFILLED){
                this.#runOne(onFulfilled, resolve, reject);
            }
            else{
                this.#runOne(onRejected, resolve, reject);
            }
        }
    }

    then(onFulfilled, onRejected){
        return new MyPromise((resolve, reject) =>{
            this.#handlers.push({
                onFulfilled,
                onRejected,
                resolve,
                reject,
            });
            this.#run();
        });
    }
}

const p = new MyPromise((resolve, reject) =>{
	setTimeout(()=>{
        reject(123);
    },1000)
});

p.then(
    null, 
    (err) =>{
        console.log('promise 失败', err);
        return 456;
    }
).then((data) =>{
    console.log("ok",data);
});

这里有三种情况:

  • 对应的回调不是函数的情况
  • 回调是一个函数的情况
  • 返回结果是一个Promise的情况

http://www.kler.cn/a/300507.html

相关文章:

  • Spring自定义BeanPostProcessor实现bean的代理Java动态代理知识
  • K8s学习
  • 使用傅里叶变换进行图像边缘检测
  • python编程-OpenCV(图像读写-图像处理-图像滤波-角点检测-边缘检测)图像变换
  • 介绍下常用的前端框架及时优缺点
  • 3. Go函数概念
  • 《实现 HTML 图片轮播效果》
  • <<编码>> 第 5 章 绕过拐弯的通信(Seeing Around Corners) 示例电路
  • 深入浅出 Ansible 自动化运维:从入门到实战
  • C++ Primer Plus(速记版)-基本语言
  • 网络安全入门教程(非常详细)从零基础入门到精通
  • 多线程:java中的实现
  • flink中slotSharingGroup() 的详解
  • MySQL索引优化与B+树【后端 14】
  • GO 闭包
  • Python | Leetcode Python题解之第396题旋转函数
  • Docker启动Mysql镜像报错问题?
  • 研究: 用于训练大型语言模型的数据集往往缺乏透明度
  • 缓存击穿问题
  • (pandas读取DataFrame列报错)raise KeyError(key) from err KeyError: (‘name‘, ‘age‘)
  • 代码随想录算法训练营day18|二叉树06
  • electron有关mac构建
  • 搜索功能技术方案
  • vs2019成功连接数据库mysql
  • 数据结构 - 顺序表
  • 跟李沐学AI:长短期记忆网络LSTM