S
SolidJS10mo ago
Mathieu

How to await on the Resource's promise?

Say users is a resource (from createResource). I would like to await for the underlying promise to complete. How could I acheive this?
const fetchUser = async (userId: string) => {
const [users] = useUsers(); // users is built with createResource
await users.???; // how to await the underlying promise?
return users().find(({ id }) => id === userId);
};
const fetchUser = async (userId: string) => {
const [users] = useUsers(); // users is built with createResource
await users.???; // how to await the underlying promise?
return users().find(({ id }) => id === userId);
};
7 Replies
marcus.og
marcus.og10mo ago
since createResource creates only async signal, i would rather reuse fetcher function in fetchUser
davedbase
davedbase10mo ago
You can await on the refetch function itself.
const [myResource, { refetch }] = createResource(fetchUser);

// Somehwere in your code...
await refetch()
const [myResource, { refetch }] = createResource(fetchUser);

// Somehwere in your code...
await refetch()
Tommypop
Tommypop10mo ago
This may be worth setting to be a derived signal or a memo, which resolves to a value when the resource resolves. (This might not fully fit your use case though)
const [users] = useUsers();
const [userId,setUserId] = createSignal(1234);
const user = createMemo(() => {
return users().find(({ id }) => id === userId());
})
const [users] = useUsers();
const [userId,setUserId] = createSignal(1234);
const user = createMemo(() => {
return users().find(({ id }) => id === userId());
})
Mathieu
Mathieu10mo ago
that will work even for the first fetching? I tried and it didn't seem to work. There is no delay between a log before await refetch() and a log after (while the request takes a while)
console.log(refetch)
console.log(refetch)
ƒ load(refetching = true) { if (refetching !== false && scheduled) return; scheduled = false; const lookup = dynamic ? dynamic() : source; loadedUnderTransition = Transition && Tr…
console.log(refetch())
console.log(refetch())
undefined
I think I should close this issue because I think it makes more sense for me to expose the fetcher, I think as @marcus.og suggested otherwise I have something like that haha
async function awaitResource(resourceFetcher) {
return await new Promise((resolve) => {
createRoot((dispose) => {
const [resource] = resourceFetcher();

createEffect(() => {
if (!resource.loading) {
resolve(resource());
dispose();
}
});
});
});
}
async function awaitResource(resourceFetcher) {
return await new Promise((resolve) => {
createRoot((dispose) => {
const [resource] = resourceFetcher();

createEffect(() => {
if (!resource.loading) {
resolve(resource());
dispose();
}
});
});
});
}
Otonashi
Otonashi10mo ago
GitHub
solid-primitives/packages/promise at main · solidjs-community/solid...
A library of high-quality primitives that extend SolidJS reactivity. - solidjs-community/solid-primitives
Otonashi
Otonashi10mo ago
though the usage is different (await until(users)).find(({ id }) => id === userId)
Mathieu
Mathieu9mo ago
Why is it okay to have createComputed inside the Promise block? Isn't the Pomise block considered async? And we can't have createEffect & co inside async code? here: https://github.com/solidjs-community/solid-primitives/blob/main/packages/promise/src/index.ts#L111 I guess the block in the promise is executed in the synchronous execution and I got confused
Want results from more Discord servers?
Add your server
More Posts
Rotate Component.Does anybody know of a Solidjs Library for rotating a component 90 degrees? (and unlike transform chPrevent route from rerenderingIs it possible to prevent a rerender going from /page1/subpage to /page1 or vice versa? I am dealiI don't understand why I get this kind of error.no matter which method you tryGET http://localhost:3000/backend An unhandled error occured: TypeError: Failed to parse URL from /asolid-motion. Children exit animation not triggering.I am using @motionone/solid for animation. I have a component wrapped in presence. This component coSolid start createResource not fetching as first routeHi all, I'm new to solid start and been trying to create a simple pokemon project that fetches pokemHelp me build a custom link component which adheres to solid's principles```typescript import { ParentComponent } from "solid-js"; import { A } from "solid-start"; import { SVG images cannot use css variable as fill, they can only use rgb colorsSVG images can have a fill value in them, like so: <svg xmlns="http://www.w3.org/2000/svg" viewBox=Delay <A> routing? UseTransition between routes?Basically i want to delay <A> navigate by few 100ms. I would also like to use useTransition between useReducer vs createReducerHello! What are the pros and cons of 1. the `useReducer` in the tutorial (https://www.solidjs.com/thow do you set attributes on `<Html>` and `<Body>` from within a route?this should happen on the server side so the attributes are set before the js is loaded