Event listeners registered for an event type may either be JavaScript functions or objects with a handleEvent property whose value is a function.

    In either case, the handler function is invoked with the event argument passed to the eventTarget.dispatchEvent() function.

    Async functions may be used as event listeners. If an async handler function rejects, the rejection is captured and handled as described in [EventTarget error handling][].

    An error thrown by one handler function does not prevent the other handlers from being invoked.

    The return value of a handler function is ignored.

    Handlers are always invoked in the order they were added.

    Handler functions may mutate the event object.

    1. function handler1(event) {
    2. console.log(event.type); // Prints 'foo'
    3. event.a = 1;
    4. }
    5. async function handler2(event) {
    6. console.log(event.type); // Prints 'foo'
    7. console.log(event.a); // Prints 1
    8. }
    9. const handler3 = {
    10. handleEvent(event) {
    11. console.log(event.type); // Prints 'foo'
    12. }
    13. };
    14. const handler4 = {
    15. async handleEvent(event) {
    16. console.log(event.type); // Prints 'foo'
    17. }
    18. };
    19. const target = getEventTargetSomehow();
    20. target.addEventListener('foo', handler1);
    21. target.addEventListener('foo', handler2);
    22. target.addEventListener('foo', handler3);
    23. target.addEventListener('foo', handler4, { once: true });