demo · v134

Cross-realm error check

The TC39 explainer’s headline use case: a library receives an unknown value and needs to know “is this an error?” — including ones constructed in another realm (an iframe, a worker, a deserialized message). instanceof Error fails across realms because each realm has its own Error intrinsic. Error.isError doesn’t.

probing Error.isError…

Run the comparison

Click run. We construct a value, hand it to instanceof Error and to Error.isError, and tally up where they disagree.

The code

// In another realm:
const otherErr = iframe.contentWindow.eval('new Error("boom")');

otherErr instanceof Error;   // false — wrong realm's Error
Error.isError(otherErr);     // true  — checks the [[ErrorData]] slot

// Same story for structured-cloned errors, worker postMessage errors,
// rejected fetch responses with errors, etc.

see also