宏任务与微任务面试题

宏任务与微任务面试题

  • 对于宏任务与微任务的执行顺序问题一直都是前端面试一到过不去的坎,这东西多看是没用的,多做就是了,刷了10来到面试题,立马顿悟。
    1
async  function  async1 ()  {
			    console.log('async1 start');
			    await  async2();
			    console.log('async1 end')
				console.log('async1 end  2')
			}
			async  function  async2 ()  {
			    console.log('async2')
			}
			console.log('script start');
			setTimeout(function ()  {
			    console.log('setTimeout')
			},  0);
			async1();
			new  Promise(function (resolve)  {
			    console.log('promise1');
			    resolve()
			}).then(function ()  {
			    console.log('promise2')
			});
			console.log('script end') 

2

 console.log('1')			
			setTimeout(function () {
			    console.log('2')
			    setTimeout(function () {
			        console.log('3')
			    },0)			
			    new Promise(function (resolve) {
			        console.log('4')			
			        for (let i = 0; i < 666; i++) {
			            i == 5 && resolve()
			        }			        
			    }).then(function () {
			        console.log('5')
			    })			
			}, 0)			
			console.log('6')			
			new Promise(function (resolve) {
			    console.log('7')			
			    for (let i = 0; i < 666; i++) {
			        i == 5 && resolve()
			    }			    
			}).then(function () {
			    console.log('8')
			})

3

setTimeout(() => {
				console.log(4);
			}, 0);

			new Promise(resolve => {
				console.log(1);
				for (let i = 0; i < 10000; i++) {
					i == 9999 && resolve();
				}
				console.log(2)
			}).then(() => {
				console.log(5)
			})

			console.log(3) 

4

setTimeout(function(){
		
			console.log('1')
		
			});	
				
			new  Promise(function(resolve){
			  console.log('2');		
			  resolve();		
			}).then(function(){
			console.log('3')		
			});	
				
			console.log('4'); 
setTimeout(() => {
				Promise.resolve().then(() => {
					console.log('1')
				})
			},0);
		
			Promise.resolve().then(() => {
				setTimeout(() => {
					console.log('2')
				},0)
			});
		
			console.log('3') 

6

setTimeout(() => {
				console.log('1')	
			},0);
		
			Promise.resolve().then(() => {
				console.log('2');
					Promise.resolve().then(() => {
						console.log('3');
				})
			})
		
			console.log('4');

7

 async function async1() {
	        console.log(1);
	        const result = await async2();
	        console.log(3);
	    }
	
	    async function async2() {
	        console.log(2);
	    }
	
	    Promise.resolve().then(() => {
	        console.log(4);
	    });
	
	    setTimeout(() => {
	        console.log(5);
	    });
	
	    async1();
	    console.log(6); 

8

console.log('1');	
			setTimeout(function() {
			    console.log('2');
			    Promise.resolve().then(function() {
			        console.log('3');
			    })
			    new Promise(function(resolve) {
			        console.log('4');
			        resolve();
			    }).then(function() {
			        console.log('5')
			    })
			})
			Promise.resolve().then(function() {
			    console.log('6');
			})
			new Promise(function(resolve) {
			    console.log('7');
			    resolve();
			}).then(function() {
			    console.log('8')
			})
	
			setTimeout(function() {
			    console.log('9');
			    Promise.resolve().then(function() {
			        console.log('10');
			    })
			    new Promise(function(resolve) {
			        console.log('11');
			        resolve();
			    }).then(function() {
			        console.log('12')
			    })
			})

9

new Promise(function(res) {
				console.log(1);
				res();
				Promise.resolve().then(function() {
					console.log(2);
				});    
				new Promise(function(res) {
					res();
				}).then(function() {
					console.log(-1);
				});
				console.log(0);
			}).then(function() {
				console.log(3);
				Promise.resolve().then(function() {
					console.log(4);
				});
			}).then(function() {
				console.log(5);
			});       
			Promise.resolve().then(function() {
				console.log(6);
				Promise.resolve().then(function() {
					console.log(7);
				});
			});   
			setTimeout(function() {
				setTimeout(function() {
					console.log(8);
				}, 0);
				Promise.resolve().then(function() {
					console.log(9);
				});
			}, 0);
			setTimeout(function() {
				console.log(10);
			}, 0);

10

console.log(1); //
			console.log(18); //
			new Promise(function(res, rej) {
				console.log(2); //
				res();
			}).then(function() {
				console.log(3); //
				Promise.resolve().then(function() {
					console.log(5);
					setTimeout(function() {
						console.log(6); //
						Promise.resolve().then(function() {
							console.log(7);
						});
						setTimeout(function() {
							console.log(8);
						}, 0);
					}, 0);
				});
			}).then(function() {
				console.log(4);
			});
			new Promise(function(res) {
				console.log(19); //
				setTimeout(function() {
					console.log(20); //
				}, 0);
			});
			Promise.resolve().then(function() {
				setTimeout(function() {
					Promise.resolve().then(function() {
						console.log(12);
					});
					console.log(13); //
				}, 0);
			});
			setTimeout(function() {
				console.log(9); //
				new Promise(function(res) {
					res();
					console.log(10);
				}).then(function() {
					console.log(11);
				});
			});
			setTimeout(function() {
				setTimeout(function() {
					setTimeout(function() {
						Promise.resolve().then(function() {
							console.log(14);
						});
						console.log(15);
					}, 0);
					console.log(16);
				}, 0);
				console.log(17); //
			}, 0);

11

console.log(1);//
      document.addEventListener("14", function () {
        console.log(14);//
      });
      new Promise(function (resolve) {
        resolve();
        console.log(2);//
        setTimeout(function () {
          console.log(3);//
        }, 0);
        Promise.resolve().then(function () {
          console.log(4);//
          setTimeout(function () {
            console.log(5);
          }, 0);
          setTimeout(function () {
            (async function () {
              console.log(6);
              return function () {
                console.log(7);
              };
            })().then(function (fn) {
              console.log(8);
              fn();
            });
          }, 0);
        });
        new Promise(function (resolve) {
          console.log(9);//
          resolve();
        }).then(function () {
          new Promise(function (resolve, reject) {
            console.log(10);
            reject();
          }).then(function () {
              setTimeout(function () {
                console.log(11);
              }, 0);
              console.log(12);
            }).catch(function () {
              console.log(13);
              var evt = new Event("14");
              document.dispatchEvent(evt);
            });
        });
      });	  
      setTimeout(function () {
        console.log(15);
        Promise.resolve().then(function () {
          console.log(16);
        });
      }, 0);
  • 6
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
好的,下面是一个宏任务微任务相关的面试题。 1. 什么是宏任务微任务? 答:宏任务微任务是 JavaScript 引擎用来管理异步任务的两种机制。 宏任务指的是在 JavaScript 引擎中排队等待执行的任务,这些任务通常是由浏览器或 Node.js 等宿主环境提供的,如定时器、事件回调等。 微任务指的是在当前任务执行完毕后需要立即执行的任务,这些任务通常是由 Promise 和 MutaionObserver 提供的,如 Promise.then()、Promise.catch()、Promise.finally() 和 MutationObserver。 2. 宏任务微任务的执行顺序是怎样的? 答:宏任务微任务执行顺序是不同的,它们都有自己的队列,而且微任务的优先级比宏任务高。 当一个宏任务被执行时,会先执行完所有的微任务,然后再继续执行下一个宏任务。如果在执行微任务的过程中产生了新的微任务,那么这些新的微任务会被插入到当前微任务队列的末尾,等待下一次执行。 举个例子:假设当前有一个宏任务 A,它包含三个微任务 X、Y 和 Z。当宏任务 A 被执行时,会先执行微任务 X,然后执行微任务 Y,最后执行微任务 Z,如果在执行微任务 Y 的过程中又产生了一个新的微任务 W,那么它会被插入到当前微任务队列的末尾,等待下一次执行。 3. 如何利用宏任务微任务实现异步操作? 答:可以使用 Promise 和 async/await 来利用宏任务微任务实现异步操作。 例如,使用 Promise 来发起一个异步请求: ```javascript function fetchData() { return fetch('https://api.example.com/data') .then(response => response.json()) .then(data => { console.log('data:', data); }) .catch(error => { console.error('error:', error); }); } ``` 在这个例子中,fetchData 函数返回一个 Promise 对象,当这个 Promise 对象被 resolve 时,会执行一系列的微任务,包括打印数据到控制台。如果发生错误,会执行一个 catch 微任务。 另外,可以使用 async/await 来简化异步操作: ```javascript async function fetchData() { try { const response = await fetch('https://api.example.com/data'); const data = await response.json(); console.log('data:', data); } catch (error) { console.error('error:', error); } } ``` 这个例子中,使用 async/await 来编写异步代码,fetchData 函数会等待 fetch 请求的结果,在结果返回后再执行后续操作。如果发生错误,会抛出一个异常,可以通过 try/catch 块来处理。在执行 async 函数时,会创建一个微任务队列来管理异步操作,确保异步操作的执行顺序正确。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值