mirror of
https://github.com/denoland/deno.git
synced 2025-08-02 18:12:39 +00:00
Fix promise reject issue (#936)
This commit is contained in:
parent
c9f95d51da
commit
45d3b8955d
14 changed files with 299 additions and 6 deletions
46
js/promise_util.ts
Normal file
46
js/promise_util.ts
Normal file
|
@ -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;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue