SolidJSS
SolidJS2y ago
130 replies
J

Creating a store with data returned from an async function

Simplifying greatly, this is what I would like to achieve:

const [tab, setTab] = createStore({
    path: await homeDir(),
    view: await userPreferences.get("preferredView")
});


I understand there is no way to get the return result of an async function in top-level code (this is within a .ts file).

Now I know some of you might suggest resources, however, I only need to use these async functions at app launch (aka once and never again), and they are all extremely fast (<10ms) so I do not need to use Suspense or add a loading indicator to my GUI.

The only solution I can think of at the moment is initializing properties like path and view with "dummy" values and then within an async-friendly initialization point such as
onMount
, populating said properties with their real values fetched from the async functions.

Using dummy values is a really crappy solution in my opinion so I would be so grateful if somebody had a neat solution to this issue. Coming from lower-level languages like Rust, the lack of control over synchronization drives me insane in these frontend frameworks.

Thanks.
Was this page helpful?