-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat: $effect.async
, $derived.async
#14305
Comments
And the expectation here would be for all reactive signals to account for effect re-run and derived value recalculation, even the ones read after awaiting? Otherwise it would be identical to the current |
The asynchronous effects and deriveds would run on a separate schedule from the regular effects and deriveds. This would have similar internal functionality to the regular scheduler, and could probably use the Scheduler API if needed, but that doesn't have full browser support. |
if you update the typings to accept an async function (btw i think we should not do such thing) but don't await the function when running the effect they would not wait for it. |
Ah, I see. Is there a reason why you're against the type updates? |
I understand the core team's reasoning behind this, but it is counter-productive. Let me explain. Fact: Synchronous code inside effects is easier to understand and troubleshoot. It is simpler and more straightforward to follow in order to, for example, determine the reactive triggers. Fact: Real world applications require tons of asynchronous operations triggered inside effects. So, while the core team's reason behind disallowing async delegates for $effect(() => {
const x = async () => { ... };
x();
});
// As opposed to allowing async:
$effect(async () => { ... }); Both are the same, work the same. So in the end, the prohibition of async on effect gains, what? In my eyes, the only gain of disallowing async is forcing users to the more verbose and unneeded syntax. Yes, users must also be taught that effect tracking only works up to the first await. I get it. |
Because async code in effects and derived is very risky. Everything after an await is not tracked and it shouldn't be the simple thing to do. Generally you shouldn't ever use async in both of those but if you really really need you can do it but it's best that you think about it before doing it. |
In the future we'll look into proper async primitives, as they will likely require some form of suspense to work correctly. As noted above, the reason why these can be problematic is because of a few reasons:
Generally speaking, we want to avoid all these caveats entirely. If you have something async, you can call from it inside your synchronous effect. Watch this space too, as in the new year we hope to build out some powerful async primitives that will really help with these use-cases. |
Oh yeah I completely forgot about this too |
Describe the problem
There have been several discussions about the issues of runes and
async
, for example, #13916. Many apps depend on async code, and it can become difficult to migrate to Svelte 5 because of this.Describe the proposed solution
It would be useful to have two asynchronous runes:
$effect.async
and$derived.async
. These functions would be asynchronous counterparts to$effect
and$derived.by
, respectively. Here's an example:You may notice that these functions do not require
await
to be called to use them, this would be because theawait
would be inserted at compile time:Importance
would make my life easier
The text was updated successfully, but these errors were encountered: