Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/ayonli_jsext/runtime.ts>addUnhandledRejectionListener

A JavaScript extension package for building strong and modern applications.
Latest
function addUnhandledRejectionListener
import { addUnhandledRejectionListener } from "https://deno.land/x/ayonli_jsext@v0.9.72/runtime.ts";

Adds a listener function to be called when an unhandled promise rejection occurs in the program, this function calls addEventListener("unhandledrejection") or process.on("unhandledRejection") under the hood.

The purpose of this function is to provide a unified way to handle unhandled promise rejections in different environments, and when possible, provide a consistent behavior of the program when an unhandled rejection occurs.

By default, when an unhandled rejection occurs, the program will log the error to the console (and exit with a non-zero code in Node.js, Deno and Bun), but this behavior can be customized by calling event.preventDefault() in the listener function.

In unsupported environments, this function is a no-op.

Examples

Example 1

import { addUnhandledRejectionListener } from "@ayonli/jsext/runtime";

addUnhandledRejectionListener((event) => {
    console.error(event.reason);
    event.preventDefault(); // Prevent default logging and exiting behavior
});

Parameters

fn: (event: PromiseRejectionEvent) => void