Promise 专题二

这是我们上一章产出的代码,不了解的童鞋先简单看一看,了解了解

class MyPromise {
   
    constructor(handle){
   
        this.status = 'pending';
        this.value = undefined ;

        // this.resolveFn = null;
        // this.rejectFn = null;
        this.resolveFnQueue = [];
        this.rejectFnQueue = [];
        // 只能通过bind 绑定this 不能用 call apply
        // 原因:这个函数的执行位置不在这,在自己的js逻辑执行
        // 使用call apply会将函数执行
        handle(this._resolve.bind(this),this._reject.bind(this))
    }

    _resolve(val){
   
        this.status = "fulfilled";
        this.value = val
        const run = ( ) => {
   
            // this.resolveFn(val)
            let cb;
            while(cb = this.resolveFnQueue.shift()){
   
                cb && cb(val)
            }
        }
        setTimeout(run)
    }
    _reject(val){
   
        this.status = "rejected";
        this.value = val
        this.rejectFn(val)
        const run = ( ) => {
   
             // this.rejectFn(val)
             let cb;
             while(cb = this.resolveFnQueue.shift()){
   
                 cb && cb(val)
             }
        }
        setTimeout(run)
    }
    then(onResolved,onRejected){
   
        // 保存回调函数,不执行
        // 执行 then 的地方为 _resolve _reject
        // this.resolveFn = onResolved;
        // this.rejectFn = onRejected;
        this.resolveFnQueue.push(onResolved)
        this.rejectFnQueue.push(onRejected)
    }
}

上一章说到setTimeout是个宏任务,而promise是个微任务,那么我们来利用其中的一个微任务
MutationObserver来改进setTimeout
先说一下MutationObserver是怎么使用的

	<script>
        let ob = new MutationObserver(function(){
   
            console.log('微任务')
        })
        ob.observe(document.body,{
   
            attributes:true
        })
        document.querySelector('body').setAttribute('MutationObserver',true)
    </script>

ob.observe的第二个属性
childList:子元素的变动
attributes:属性的变动
characterData:节点内容或节点文本的变动
subtree:所有下属节点(包括子节点和子节点的子节点)的变动
想要观察哪一种变动类型,就在option对象中指定它的值为true。需要注意的是,不能单独观察subtree变动,必须同时指定childList、attributes和characterData中的一种或多种。
所以修改后的代码为

class MyPromise {
   
    constructor(handle){
   
        this.status = 'pending';
        this.value = undefined ;

        // this.resolveFn = null;
        // this.rejectFn = null;
        this.resolveFnQueue = [];
        this.rejectFnQueue = [
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值