Avoid Async Effects In Svelte

Поделиться
HTML-код
  • Опубликовано: 18 янв 2025

Комментарии • 47

  • @hex7557
    @hex7557 Месяц назад +12

    Thanks so much! I've been working on an application that uses Svelte 5 and accesses a database, and finding any information on how to handle things asynchronously has been a nightmare.

  • @DeviantFox
    @DeviantFox Месяц назад +5

    I was not expecting THIS much depth in this short of a video lol

  • @huakun
    @huakun Месяц назад +7

    what if the returned cleanup function depends on the async function?
    e.g. the async function returns an unlistener.
    The solution I can think of is to store the unlistener outside as a mutable variable with let, then call it in the clean up function.

    • @JoyofCodeDev
      @JoyofCodeDev  Месяц назад +1

      sounds good to me

    • @huakun
      @huakun Месяц назад

      the use of unlisten is very common in Tauri apps. I use onMount and onDestroy, hope they don’t get removed

  • @honk_tm
    @honk_tm Месяц назад +1

    So we are back to writing vars without any other purpose than forcing a refresh on an effect, just as we had with $: before. I thought Svelte 5 was made to fix that horrendous thing...

    • @JoyofCodeDev
      @JoyofCodeDev  Месяц назад +1

      you should avoid effects and `$:` was used for both deriving values and side effects but now you have `$derived` and `$effect`

  • @davidpierrumentor
    @davidpierrumentor Месяц назад

    Hi, I love your video and I the feeling you put in it. May I suggest a video on testing a simple application with svete and vitest ? (without and with sveltekit ?). I didn't found any great video with svelte 5.

  •  Месяц назад +2

    I think the "solution" at 355s (5:55) has the same issue, that count is not tracked.

    • @dumboluzz
      @dumboluzz Месяц назад +2

      I was thinking the same. Gotta try this out once I get home

    •  Месяц назад +1

      @dumboluzz I did already 😉

    •  Месяц назад +1

      btw I think the solution is to pass count into the async function as parameter

    • @JoyofCodeDev
      @JoyofCodeDev  Месяц назад +3

      you can move `count` before `await` otherwise Svelte won't track it

    •  Месяц назад +3

      @@JoyofCodeDev sure, but in real cases probably you want to use the value inside the async function. so passing as parameter can be seen as "moving above async"

  • @mylastore
    @mylastore Месяц назад

    Don't you need await to fulfill the promise/async ?

  • @j.m.manhard
    @j.m.manhard Месяц назад +2

    First 🎉

  • @StephanHoyer
    @StephanHoyer Месяц назад +3

    Or even better, just avoid svelte

    • @realderek
      @realderek Месяц назад +11

      If you don't like Svelte why waste your time watching and commenting on Svelte videos?

    • @aramp
      @aramp Месяц назад +3

      if you think front-end frameworks are useless you probably haven't worked on anything complex

    • @oskarkottom8365
      @oskarkottom8365 Месяц назад +4

      Says the react user

    • @StephanHoyer
      @StephanHoyer Месяц назад

      Yes I don't like because it's such a dumb idea and I don't understand why anyone thinks it is a good one. Sure, for anything simple it might be worth it. But for complex apps the framework part is only a fraction of the resulting code so why not using a real framework in the first place. And one which actually works like javascript not some complex custom language that relies on magic left and right and that isn't debuggable at all.
      And no I don't hate frameworks. I use a really great one called mithril that is simple yet powerful and small.
      And I also build a pretty complex app with it the is comparable to Google docs or canva. And it works like a charm for years now.

    • @StephanHoyer
      @StephanHoyer Месяц назад

      @realderek I generally also watch videos about other frameworks to learn something that I can use in my framework of choice. May there are some good ideas there too

  • @RohitKumarAnkam
    @RohitKumarAnkam Месяц назад

    Hi everyone,
    ruclips.net/video/fR6DFKq13J0/видео.html
    what rich is talking about here?

    • @JoyofCodeDev
      @JoyofCodeDev  Месяц назад +1

      I think Rich is talking about suspense and fetching data at the component level like RSC, which Svelte right now doesn't support, but they're thinking about it