clearTimeout right after setTimeout — what does this code print?
const timer = setTimeout(() => console.log('tick'), 0)
clearTimeout(timer)
console.log('done')- A.done
- B.done tick
- C.(nothing is printed)
- D.tick done
Answer: A
Prints only done. setTimeout returns a handle; clearTimeout(handle) runs synchronously right after, so the timer is cancelled before the event loop gets a chance to run it. The tick callback is never called. (FREE)