generators inside DOM events

# Andrea Giammarchi (11 years ago)

I am not sure this has been discussed already but I wonder what would happen if yield is used inside an event such 'beforeunload', 'unload', or even 'click' and others DOM related events.

Main concerns:

  1. it's a UA trap potentially making impossible to leave a page or complete a user meant action
  2. not even transpilers can solve cases like this (i.e. a still valid event eventually stopped after some generator logic where if simulated the event would be expired at the time the function will be invoked)
# Domenic Denicola (11 years ago)

What? That would just cause the event handler function to return a generator object, which the browser would not use or do anything with. It would have no effect.

# Andrea Giammarchi (11 years ago)

Sorry, I explained it badly ... let me try again:

what if a DOM event handler uses/creates/invokes inside its function body a generator?

Will the event pause until this will be satisfied?

a.addEventListener('click', function(e){ if(methodThatInvokesGenerator()) e.stopPropagation(); });

Is this a concern?

# Brandon Benvie (11 years ago)

Calling a generator function just creates a paused generator. Generators can't pause callers, just themselves (via yield).

# Andrea Giammarchi (11 years ago)

OK, I have overlooked at this ... so the following code won't have any side effect, correct?

addEventListener('beforeunload', function (e) {
  (function*() {
    while (true) yield null;
  }());
});
# Andrea Giammarchi (11 years ago)

sorry, actually the right example was with while (true) yield evt; but that's the same of async callback, the event is gone.

Well, everything good then ^_^

Thanks again

# Brendan Eich (11 years ago)

'yield' in an HTML event attribute value should be a free variable reference, since the attribute value is taken as the source of a function body, not of a function* body.