| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- </head>
- <body>
- <script>
- // promise 可以直接后边写 then和catch方法
- // new Promise((resolve,reject)=>{
- // resolve("3")
- // }).then((res)=>{
- // console.log(res);
- // }).catch((err)=>{
- // console.log(err);
- // })
- // 事件循环
- // 事件循环的执行顺序是:
- // js先执行同步代码 如果运行过程中遇到异步操作,会将异步操作放到任务队列中
- // 等同步代码执行完毕后,会从任务队列中取任务执行 (谁处于待运行状态)
- // 如果任务队列中有多个异步任务可以运行状态,那么js在执行时会有一个优先级
- // 优先级分为(宏任务和微任务)宏任务:setTimeout、setInterval 微任务:Promise.then
- // 先执微任务,再执行宏任务
- // 如果执行中再次遇到异步任务,会将异步任务放到任务队列中,等待后续执行
- // 重复以上过程,直到任务队列中没有任务可执行
- // 这个过程称为事件循环 (event loop)
-
- console.log("1");
- setTimeout(() => {
- console.log("2");
- }, 0);
- new Promise((resolve, reject) => {
- setTimeout(() => {
- resolve("3")
- }, 0);
- }).then((res) => {
- console.log(res);
- })
- console.log("4");
- </script>
- </body>
- </html>
|