宏任务 和 微任务 执行顺序

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>中国人</title>
</head>
<body></body>
<script>

    setTimeout(() => { // 宏任务
        console.log("setTimeout");
    }, 0);

    new Promise(resolve => {
        resolve(); // 成功通知 就会把任务加到微任务队列里面
        console.log("promise");
    }).then(value => console.log("成功"));
    console.log("中国人"); // 同步

    // 同步执行 优先级最高
    // 1 同步任务
    // 2 微任务
    // 3 宏任务

    // 看到setTimeout 放到任务队列的宏任务里准备执行
    
    // 先走主线程 然后走微任务 最后走宏任务

    //  promise
    //  中国人
    //  成功
    //  setTimeout
</script>
</html>
发布了63 篇原创文章 · 获赞 5 · 访问量 826

猜你喜欢

转载自blog.csdn.net/wuj1935/article/details/105423443