From d96624151c6706558f7377cb1611b04516ea696e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BF=B7=E6=B8=A1?= Date: Fri, 6 Aug 2021 15:09:39 +0800 Subject: [PATCH 1/3] fix(async): let async exception of `deadline` can be caught --- async/deadline.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/async/deadline.ts b/async/deadline.ts index 12a49f0ff384..42a712141cc6 100644 --- a/async/deadline.ts +++ b/async/deadline.ts @@ -13,6 +13,5 @@ export class DeadlineError extends Error { export function deadline(p: Promise, delay: number): Promise { const d = deferred(); const t = setTimeout(() => d.reject(new DeadlineError()), delay); - p.finally(() => clearTimeout(t)); - return Promise.race([p, d]); + return Promise.race([p, d]).finally(() => clearTimeout(t)); } From 60044083bded746461f98746d1d55c3d6d61cbf9 Mon Sep 17 00:00:00 2001 From: justjavac Date: Sat, 7 Aug 2021 14:18:11 +0800 Subject: [PATCH 2/3] add a test case for rejected promise --- async/deadline_test.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/async/deadline_test.ts b/async/deadline_test.ts index d12b816f11ea..217cad3044ce 100644 --- a/async/deadline_test.ts +++ b/async/deadline_test.ts @@ -18,3 +18,16 @@ Deno.test("[async] deadline: throws DeadlineError", async () => { }, DeadlineError); clearTimeout(t); }); + +Deno.test("[async] deadline: thrown when promise is rejected", async () => { + const p = deferred(); + const t = setTimeout(() => p.reject(new Error("booom")), 100); + await assertThrowsAsync( + async () => { + await deadline(p, 100); + }, + Error, + "booom", + ); + clearTimeout(t); +}); From 1062b37fe3bd64f3a8ef862c111d800a271d986b Mon Sep 17 00:00:00 2001 From: justjavac Date: Sat, 7 Aug 2021 14:19:21 +0800 Subject: [PATCH 3/3] fix time --- async/deadline_test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/async/deadline_test.ts b/async/deadline_test.ts index 217cad3044ce..d024c7ac8984 100644 --- a/async/deadline_test.ts +++ b/async/deadline_test.ts @@ -24,7 +24,7 @@ Deno.test("[async] deadline: thrown when promise is rejected", async () => { const t = setTimeout(() => p.reject(new Error("booom")), 100); await assertThrowsAsync( async () => { - await deadline(p, 100); + await deadline(p, 1000); }, Error, "booom",