美文网首页
Promise-04 微任务和宏任务

Promise-04 微任务和宏任务

作者: 呆桃冲鸭冲鸭 | 来源:发表于2020-09-11 07:10 被阅读0次

宏任务: setTimeout、setInterval...
微任务:Promise、MutationObserver...

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>原生promise</title>
</head>
<body>
    
</body>
<!-- 宏任务  script-->
<script>
    console.log("Promise=====444");
    // 宏任务
    setTimeout(()=>{
        console.log("Promise=====333");
    },0);
    // 三种状态 1.pending 2.fulfilled 3.rejected
    let p = new Promise((resolve,reject)=>{
        console.log("Promise=====111");
        resolve("suc...");
        // rejected("err...");
    });
    //微任务
    p.then(res=>{
        console.log("Promise=====222");
    },err=>{
        console.log(err);
    });
    // Promise=====444
    // Promise=====111
    // Promise=====222
    // Promise=====333
</script>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>my</title>
</head>
<body>
    <p>test my</p>
</body>
<script type="module">
    //  type="module" 严格模式 
    import MyPromise from './promise.js';
    console.log("Promise=====444");
    // 宏任务
    setTimeout(()=>{
        console.log("Promise=====333");
    },0);
    // 三种状态 1.pending 2.fulfilled 3.rejected
    let myP = new MyPromise((resolve,reject)=>{
        console.log("Promise=====111");
        resolve("suc...");
        // reject("err...");
    });
    //微任务
    myP.then(res=>{
        console.log("Promise=====222");
    },err=>{
        console.log(err);
    });

</script>
</html>
export default class MyPromise{
    constructor(handle){
        this.state = "pending";
        this.result = undefined;
        this.resolveFnArr = [];
        this.rejectFnArr = [];
        handle(this._resolve.bind(this),this._reject.bind(this));
    };
    _resolve(val){
        this.state = "fulfilled";
        this.result = val;
        // 执行then的回调 变成微任务执行
        const run = () => {
            let cb;
            while(cb = this.resolveFnArr.shift()){
                cb && cb(val);
            };
        };
        const observer = new MutationObserver(run);
        observer.observe(document.body,{attributes: true});
        document.body.setAttribute("my",Math.random());
    };
    _reject(val){
        this.state = "rejected";
        this.result = val;
        // 执行then的回调 变成微任务执行
        const run = () => {
            let cb;
            while(cb = this.rejectFnArr.shift()){
                cb && cb(val);
            };
        };
        const observer = new MutationObserver(run);
        observer.observe(document.body,{attributes: true});
        document.body.setAttribute("my",Math.random());
    };
    then(onResolved,onRejected){
        // 异步onResolved,onRejected不会立即执行;是在调取_resolve、_reject再执行
        // 保存onResolved、onRejected
        this.resolveFnArr.push(onResolved)
        this.rejectFnArr.push(onRejected)
    };
}
打印结果

相关文章

  • Promise-04 微任务和宏任务

    宏任务: setTimeout、setInterval...微任务:Promise、MutationObserve...

  • 微任务和宏任务@小四@王云飞

    微任务和宏任务 微任务 和 宏任务 表示异步任务的两种分类。 微任务(microtask)和宏任务(macrota...

  • 宏任务和微任务

    [js 宏任务和微任务] .宏任务(macrotask )和微任务(microtask ) macrotask 和...

  • 宏任务 和 微任务

    宏任务: 当前调用栈执行的代码成为宏任务,(主代码块和定时器)也或者宿主环境提供的叫宏任务 这些任务包括: 渲染事...

  • 宏任务和微任务

    介绍这个之前, 建议先了解一下事件循环[https://www.jianshu.com/p/106867eee55...

  • 宏任务和微任务

    浏览器是多线程执行代码,渲染的。但是浏览器只给JS一个线程来执行,因此JS是单线程。因此代码都是同步执行的,但是J...

  • # 宏任务和微任务

    首先说明 首先在JavaScript中,有同步代码和异步代码.这点很清晰. 代码的执行优先级顺序是,同步代码执行优...

  • 微任务和宏任务

    JS是单线程的,可以把这个线程叫做主线程,主线程中包含宏任务队列和微任务队列,宏任务所在的队列就叫宏任务队列,微任...

  • 宏任务和微任务

    所谓微任务和宏任务 宏任务::常见的定时器,用户交互事件等等。可以理解是每次执行栈执行的代码就是一个宏任务。(宏任...

  • 2018-08-15 微任务 宏任务 MicroTask Mac

    微任务和宏任务 微任务(Microtask)宏任务(Microtask)process.nextTickPromi...

网友评论

      本文标题:Promise-04 微任务和宏任务

      本文链接:https://www.haomeiwen.com/subject/csypektx.html