svecosystem / runed

Magical utilities for your Svelte applications (WIP)
https://runed.dev
MIT License
387 stars 21 forks source link

Previous awaited value #82

Closed notchatbot closed 1 month ago

notchatbot commented 1 month ago

Describe the feature in detail (code, mocks, or screenshots encouraged)

I have the following situation

let previousValueAwaited = $state()

const someAsyncFunction = async () => { 
const response = await fetch something
previousValueAwaited = await response.json()
}
const valuePromise = $derived(async someAsyncFunction )

There may be a cool application for Previous that should always be equal to the last awaited value.

What type of pull request would this be?

Enhancement

Provide relevant links or additional information.

No response

abdel-17 commented 1 month ago

The problem with async and runes is that dependencies cannot be determined in an async context. Can you describe the feature in more detail? Perhaps with a REPL example?

TGlide commented 1 month ago

Can't you just do something like this?

notchatbot commented 1 month ago

It worked, but I wanted to achieved that "magical" dx that runes have without a workaround.

Here is the repl

Important: There is a small bug when clicking quickly the button that previous and current values are beign shown at the same time.

TGlide commented 1 month ago

I think this is a Svelte bug to be honest. Will check.

TGlide commented 1 month ago

Anyways, is something like this what you wanted? REPL

TGlide commented 1 month ago

Indeed, it's a Svelte bug, will be fixed by https://github.com/sveltejs/svelte/pull/11989

notchatbot commented 1 month ago

Yeap Await blocks are kind of buggy. Yes! It does work. I guess that the most important part of this behaviour is "in case you are a promise then show the previous value" (Which can be achieve with the $effect that you added, no need for Previous but still might be useful though).

huntabyte commented 1 month ago

Is this good to close now that the Svelte issue has been resolved? @TGlide @notchatbot

notchatbot commented 1 month ago

Closed!