From 3c5233297a20b68d87f6ae5aa541e61783067fed Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Mon, 22 Mar 2021 02:58:19 +0100 Subject: [PATCH] Avoid duplicate initial fetching with asyncReactive --- ts/graphs/asyncReactive.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/ts/graphs/asyncReactive.ts b/ts/graphs/asyncReactive.ts index d07b71749..394acc3a7 100644 --- a/ts/graphs/asyncReactive.ts +++ b/ts/graphs/asyncReactive.ts @@ -13,14 +13,15 @@ function useAsyncReactive( ): AsyncReativeData { const promise = derived( dependencies, - (_, set) => set(asyncFunction()), - asyncFunction() + (_, set: (value: Promise | null) => void) => set(asyncFunction()), + // initialize with null to avoid duplicate fetch on init + null ); const value = derived( promise, ($promise, set: (value: T) => void) => { - $promise.then((value: T) => set(value)); + $promise?.then((value: T) => set(value)); }, null ); @@ -28,7 +29,7 @@ function useAsyncReactive( const error = derived( promise, ($promise, set: (error: E | null) => void) => { - $promise.catch((error: E) => set(error)); + $promise?.catch((error: E) => set(error)); return () => set(null); }, null