Recently, I came across a mutation observer in some TypeScript code that has left me puzzled. This particular implementation of a promise within the mutation observer seems unconventional to me:
const observer = new MutationObserver((mutations: MutationRecord[]) => {
Promise.resolve().then(someNextMethod)
this.somethingThatTakesAWhile()
})
observer.observe(HTMLtarget)
Surprisingly, when the observation is triggered, the someNextMethod
executes after this.somethingThatTakesAWhile()
. I'm struggling to grasp how the promise in this scenario does not have any arguments yet still knows how to resolve. Can someone shed light on the underlying mechanics of this code? The sequence of execution here has got me intrigued. Thank you for your help!