Skip to content

Revisiting globalThis as an EventTarget #51372

@jasnell

Description

@jasnell

In browsers, deno, bun, workers and other runtimes, globalThis implements the Web platform standard EventTarget API. While there are differences in the specific events emitted at the global scope, the unhandledrejection and rejectionhandled events are common to all of these runtimes. In Node.js our globalThis, of course, does not implement EventTarget and implements unhandled rejection events using EventEmitter instead. This ends up causing some interop headaches for library authors who have to special case handling of these events across runtimes.

We've discussed this before but I'd like to re-open the discussion for consideration around the questions:

  • Should Node.js globalThis implement EventTarget
  • Should we emit the Web standard unhandledrejection and rejectionhandled events on globalThis and either (a) deprecate or (b) mark as legacy the equivalent events on process?
  • Should we support other Web standard events on globalThis including:
    • beforeunload (equivalent to process' beforeExit event)
    • error (equivalent to process' uncaughtException event
    • load
    • message (equivalent to process' message event)
    • messageerror
    • etc

@nodejs/tsc @nodejs/web-standards

Context: this came up in a WinterCG discussion around whether interoperable/consistent handling of unhandled errors and rejections should be covered by the WinterCG common minimum API spec.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions