nextTick原理

2020-09-05  本文已影响0人  罗不错
let tag =false
 let arr = []
 function batEvent(){
   arr.forEach(fn=>fn())
   tag=false
 }

 function nextTick(fn){
   arr.push(fn)
   if(!tag){
     tag=true
     Promise.resolve().then(batEvent)
   }
 }

 nextTick(()=>{
   console.log('a')
 })
 nextTick(()=>{
   console.log('b')
 })
 console.log('1')
上一篇 下一篇

猜你喜欢

热点阅读