According to MDN, messages in the JavaScript event loop "run to completion".
(https://developer.mozilla.org/docs/Web/JavaScript/Event_loop#run-to-completion)
But I have created a case where this does not seem to happen.
abortController1 = new AbortController()
abortController2 = new AbortController()
abortController1.signal.addEventListener("abort", () =>
{
console.log("abort 1 start")
abortController2.abort()
console.log("abort 1 end")
})
abortController2.signal.addEventListener("abort", () =>
{
console.log("abort 2")
})
abortController1.abort()
Output:
abort 1 start
abort 2
abort 1 end
I was expecting to see this output:
abort 1 start
abort 1 end
abort 2
Can someone please explain what's going on here?
EDIT (possible clues) -
DrainDirectTasks - https://searchfox.org/mozilla-central/rev/94c62970ba2f9c40efd5a4f83a538595425820d9/xpcom/threads/nsThread.cpp#1094
EventTarget.dispatchEvent() - https://developer.mozilla.org/docs/Web/API/EventTarget/dispatchEvent
When an event is triggered directly by code, it runs the listeners synchronously. The MDN documentation for dispatchEvent() explains this:
Unlike "native" events, which are fired by the browser and invoke event handlers asynchronously via the event loop,
dispatchEvent()invokes event handlers synchronously. All applicable event handlers are called and return beforedispatchEvent()returns.
While this doesn't mention abort() specifically, I see no reason why it wouldn't be treated similarly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With