-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Add async functions to the $effect parameter type #10498
Comments
Unlike |
This problem only occurs in asynchronous functions in dependencies that are read after an await, and the same problem exists in non-asynchronous functions in dependencies that are not read immediately on the first execution Callbacks, if blocks, try catch, ternaries, switch statements, labels, all of these can cause the same problem I believe the problem is the opposite, not adding typing for asynchronous functions implies that they do not work regardless of use, which is not true The only solution to this problem is good documentation |
For most of those it's a feature. The effect only reruns if its actual dependencies change. Related: |
Came here to say the same thing! I was surprised while migrating some Svelte 4 code from Reading through previous responses, I can see the issues with that, but I think it should be possible if the compiler knows that we've I don't know much about the internals, but these are my 2c :p Edit: I just realized that it works just fine if I ignore TypeScript errors, so it's just a typing issue. |
Effects can't take async functions, so closing this issue. |
Can't or shouldn't? |
And as already mentioned, one of the uses of $effect is to be an alternative to onMount |
@Thiagolino8 Maybe we should change the documentation around this as I think we can keep |
There's one thing we could do: allow async functions, but then they cannot return anything because people might return something expecting the cleanup function to be called. I'm not sure it's worth it though |
@dummdidumm I thought about that but if you have an async function, it's so you can |
Describe the problem
The onMount function typing allows the reception of an asynchronous function as long as this function does not return a cleanup
The typing of the $effect rune does not allow the same, even though it is much more useful for it than for onMount and even though $effect is being marketed as an alternative to onMount
Describe the proposed solution
Change the typing to, just like onMount, allow asynchronous function as a callback as long as this callback does not return a cleanup
Importance
nice to have
The text was updated successfully, but these errors were encountered: