Skip to content

Feat: $effect.async, $derived.async #14305

Open
@Ocean-OS

Description

@Ocean-OS

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:

let fetched = $derived.async(async()=>{
    let res = await fetch('./example.json');
    return await res.json(); 
});
$effect.async(async () =>{
    let asyncValue = await getAsyncValue();
    if(asyncValue === preferredAsyncValue) {
        runCallback(); 
    } 
}); 

You may notice that these functions do not require await to be called to use them, this would be because the await would be inserted at compile time:

let thing = $derived.async(asyncValue);
$inspect(thing); 
//turns into
let thing = await $.derived_async(asyncValue); 
$.inspect_async(async() =>[await $.get_async(thing)]); 

Importance

would make my life easier

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions