summaryrefslogtreecommitdiff
path: root/tests/specs/node/rejection_handled_web_process/rejection_handled_web_process.ts
blob: c7957082f7c68262671748ffc6ab70a4b9fd8db0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import chalk from "npm:chalk";
import process from "node:process";

console.log(chalk.red("Hello world!"));

const { promise, resolve } = Promise.withResolvers();

globalThis.addEventListener("unhandledrejection", (e) => {
  console.log('globalThis.addEventListener("unhandledrejection");');
  e.preventDefault();
});

globalThis.addEventListener("rejectionhandled", (_) => {
  console.log("Web rejectionhandled");
});

process.on("rejectionHandled", (_) => {
  console.log("Node rejectionHandled");
  resolve();
});

const a = Promise.reject(1);
setTimeout(() => {
  a.catch(() => console.log("Added catch handler to the promise"));
}, 100);

const exitTimeout = setTimeout(() => {
  console.error("timeout expired");
  Deno.exit(1);
}, 30_000);

promise.then(() => {
  console.log("Success");
  clearTimeout(exitTimeout);
});