宏任务与微任务面试题

宏任务与微任务面试题

  • 对于宏任务与微任务的执行顺序问题一直都是前端面试一到过不去的坎,这东西多看是没用的,多做就是了,刷了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);
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值