From 0e441b9fb8f9eac18fdcd63663a46fd6c25d3e53 Mon Sep 17 00:00:00 2001 From: Jon Koops Date: Mon, 3 Jul 2023 15:43:08 +0200 Subject: [PATCH] Properly handle execeptions thrown by Promises in the `usePromise()` hook (#21408) --- js/apps/account-ui/src/utils/usePromise.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/js/apps/account-ui/src/utils/usePromise.ts b/js/apps/account-ui/src/utils/usePromise.ts index 488271c8db..d9e40b06f8 100644 --- a/js/apps/account-ui/src/utils/usePromise.ts +++ b/js/apps/account-ui/src/utils/usePromise.ts @@ -52,15 +52,23 @@ export function usePromise( const { signal } = controller; async function handlePromise() { - const value = await factory(signal); + // Try to resolve the Promise, if it fails, check if it was aborted. + try { + callback(await factory(signal)); + } catch (error) { + // Ignore errors caused by aborting the Promise. + if (error instanceof Error && error.name === "AbortError") { + return; + } - if (!signal.aborted) { - callback(value); + // Rethrow other errors. + throw error; } } handlePromise(); + // Abort the Promise when the component unmounts, or the dependencies change. return () => controller.abort(); }, deps); }