summaryrefslogtreecommitdiff
path: root/js/promise_util.ts
diff options
context:
space:
mode:
authorKevin (Kun) "Kassimo" Qian <kevinkassimo@gmail.com>2018-10-12 11:22:52 -0700
committerRyan Dahl <ry@tinyclouds.org>2018-10-12 14:22:52 -0400
commit45d3b8955de628db0ef051eeb8e351837b4a3b3e (patch)
tree252bab755760d86cab48502abdabe6ff0c2b9af2 /js/promise_util.ts
parentc9f95d51da9f6075b1f28a842dc830ec5fe7a30e (diff)
Fix promise reject issue (#936)
Diffstat (limited to 'js/promise_util.ts')
-rw-r--r--js/promise_util.ts46
1 files changed, 46 insertions, 0 deletions
diff --git a/js/promise_util.ts b/js/promise_util.ts
new file mode 100644
index 000000000..a550bc388
--- /dev/null
+++ b/js/promise_util.ts
@@ -0,0 +1,46 @@
+import { PromiseRejectEvent } from "./libdeno";
+
+/* tslint:disable-next-line:no-any */
+const rejectMap = new Map<Promise<any>, string>();
+// For uncaught promise rejection errors
+
+/* tslint:disable-next-line:no-any */
+const otherErrorMap = new Map<Promise<any>, string>();
+// For reject after resolve / resolve after resolve errors
+
+export function promiseRejectHandler(
+ error: Error | string,
+ event: PromiseRejectEvent,
+ /* tslint:disable-next-line:no-any */
+ promise: Promise<any>
+) {
+ switch (event) {
+ case "RejectWithNoHandler":
+ rejectMap.set(promise, (error as Error).stack || "RejectWithNoHandler");
+ break;
+ case "HandlerAddedAfterReject":
+ rejectMap.delete(promise);
+ break;
+ default:
+ // error is string here
+ otherErrorMap.set(promise, `Promise warning: ${error as string}`);
+ }
+}
+
+// Return true when continue, false to die on uncaught promise reject
+export function promiseErrorExaminer(): boolean {
+ if (otherErrorMap.size > 0) {
+ for (const msg of otherErrorMap.values()) {
+ console.log(msg);
+ }
+ otherErrorMap.clear();
+ }
+ if (rejectMap.size > 0) {
+ for (const msg of rejectMap.values()) {
+ console.log(msg);
+ }
+ rejectMap.clear();
+ return false;
+ }
+ return true;
+}