useAsyncComputed$ #240
Replies: 3 comments 2 replies
-
It should be noted that this differs from useTask$+useSignal because the resulting signal never seems to be undefined. If you try to read the signal while it's still loading, it will throw a Promise and re-render later when the value is known. Also, we should change const computed = useAsyncComputed$(async ({ track }) => {
const value = track(() => someValue.value);
return await someAsyncOperation(value);
});
return (
<Resource
value={computed}
onPending={() => <div>Loading...</div>}
onResolved={(data) => <div>{data}</div>}
onRejected={(error) => <div>Error: {error.message}</div>}
/>
);
// also
return (
<>
{computed.loading
? <div>Loading...</div>
: computed.error
? <div>Error: {computed.error.message}</div>
: <div>{computed.value}</div>}
</>
); It would also be nice to have a "stale while revalidate", meaning the first time it should show "loading" but after that it should only show the computed values. For that, maybe we can add a const computed = useAsyncComputed$(async ({ track }) => {
const value = track(() => someValue.value);
return await someAsyncOperation(value);
});
return (
<>
{computed.error
? <div>Error: {computed.error.message}</div>
: computed.resolved
? <div>{computed.resolved}</div>
: <div>Loading...</div>}
</>
); |
Beta Was this translation helpful? Give feedback.
-
I would love to see this |
Beta Was this translation helpful? Give feedback.
-
Here's another suggestion for a syntax that is like
(it could be named differently, this is just an illustration of the concept) I like the fact that the state management of the computed value is being handled by a "control flow" component instead of the end user, it's a bit nicer DX in my opinion. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
What is it about?
Create computed signals that can handle asynchronous operations.
Async Computed Signals
The
useAsyncComputed$
hook allows you to create computed signals that can handle asynchronous operations. This is useful when you need to compute a value that depends on asynchronous data or operations.Basic Usage
Features
track
function to track dependencies, similar to a useTask$ hookAPI
useAsyncComputed$
Parameters
qrl
: A QRL that computes the value. It receives a context object with:track
: A function to track dependenciescleanup
: A function to cleanup the async operationReturns
An
AsyncComputedReadonlySignal<T>
that:value
property containing the computed value, because it is a readonly signalloading
property containing the loading boolean stateerror
property containing the error object if the computation failed, or null if successfulExample with Dependencies
Implementation Details
The async computed signal implementation:
track
functioncleanup
functionMigration from useResource$
The
useResource$
API will be deprecated in favor ofuseAsyncComputed$
. This new API provides a more intuitive and flexible way to handle asynchronous computations while maintaining the same core functionality. The migration path is straightforward:Key benefits of migrating to
useAsyncComputed$
:Limitations
Future Improvements
Feedback
We welcome feedback on this feature. Please let us know:
Links / References
No response
Beta Was this translation helpful? Give feedback.
All reactions