Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

微任务、宏任务与Event-Loop #29

Open
YutHelloWorld opened this issue May 26, 2022 · 1 comment
Open

微任务、宏任务与Event-Loop #29

YutHelloWorld opened this issue May 26, 2022 · 1 comment

Comments

@YutHelloWorld
Copy link
Owner

微任务、宏任务与Event-Loop

ajax是宏任务还是微任务?

@YutHelloWorld
Copy link
Owner Author

<!DOCTYPE html>
<html lang="zh-cn">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body></body>
  <script src="https://unpkg.com/pubsub-js"></script>
  <script>
   const data = {
      syncNum: 0,
      asyncNum: 0,
    };
    // 异步
    PubSub.subscribe("async", (msg, data) => {
      console.log('5.异步');
      data.asyncNum++;
    });
    // 同步
    PubSub.subscribe("sync", (msg, data) => {
      console.log('1.同步');
      data.syncNum++;
    });
    PubSub.publish("async", data);
    PubSub.publishSync("sync", data);
    console.log("2.========");
    // 模拟请求
    Promise.resolve(data).then(({ syncNum, asyncNum }) => {
      console.log('4.异步微任务同步数值', syncNum); // 1
      console.log('4.异步微任务异步数值', asyncNum);  // 0
    });
    setTimeout(() =>{
      console.log('6.setTimeout异步宏任务', data.asyncNum) // 1
    })
    console.log('3.同步任务同步数值', data.syncNum); // 1
    console.log('3.同步任务异步数值', data.asyncNum);  // 0
  </script>
</html>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant