Home What happens when new listener is attached when an event is pending?
Reply: 0

What happens when new listener is attached when an event is pending?

user1080
1#
user1080 Published in May 21, 2018, 6:43 pm

Say I have a web worker _worker, and I attach a listener to it like so:

_worker.addEventListener('message', (event) => {
    window["test"] = event.data||"foo"
});

Let's say I later conditionally attach another listener inside a promise like so:

if (window["test"] === undefined) {
    _worker.addEventListener('message', (event) => {
        window["test"] = event.data || "foo";
        resolve(window["test"])
    });
}
else {
    resolve (window["test"]);
}

Basically the first listener's job is to set window["test"] to the event data. The promise generator checks to see if window["test"] is already set by the first listener; if not, we attach another listener to resolve the promise when the worker message event is fired.

But what if, as we are attaching the second listener, the worker has already fired a message event, and the event loop is waiting to fire the first listener? Will the second listener be included in the list of listeners to be notified of the event?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.276635 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO