Discover and read the best of Twitter Threads about #await

Most recents (5)

If you have a promise waiting for your data, {#await} block makes it easy to express your content in the loading πŸ•‘, resolved βœ… and rejected ❌ state

So, let's talk about the {#await} block in @sveltejs

πŸ‘‡πŸ§΅[THREAD]
Here's the general structure for the {#await} block

πŸ•‘ {#await}
βœ… {:then}
❌ {:catch}
πŸ‘‹ {/await}

[1/4] <script>   let promise; </s...
All the states are optional, so you could have

😜 no loading state
πŸ˜† no resolved state
πŸ˜… no rejected state

[2/4] <script>   let promise; </s...<script>   let promise; </s...<script>   let promise; </s...
Read 8 tweets
HTML doesn't have a way of expressing logic, like conditionals and loops. @sveltejs does.

Let's take a look at the 1st logic block in @sveltejs, πŸ‘‰ {#if} πŸ‘ˆ

πŸ‘‡πŸ§΅ [THREAD]
If you prefer watching video, the following thread is a summary from my @YouTube video
πŸ’‘{#if} is our 1st logic logic block in @sveltejs
πŸ’‘it allow us to conditional render content
πŸ’‘if comes with else, else if, just like you would expect in JavaScript <script>   let value = 42; </script>  {#if value === 42}   <
Read 8 tweets
I have a couple of new gotchas for people doing Svelte:

1. fade:out, or almost any "out" transition effect is death. Avoid at all costs.

2. {#await} on a promise is great, but if you update the promise it reloads the *entire* DOM at that point. Put it into a component.
For #1, the demo involves this fade:out on the Spinner I use to show the video is loading. Seems harmless *until* you want to transition to an entire new page/component, say with svelte-spa-router.
You can see it happen here. I click the link for a related video, and you can see the current video has to "fade out", preventing that page from leaving the DOM, so it looks like both pages load at the same time. I'm at a point now where I don't use *any* out transitions.
Read 10 tweets
Fetch a random Star Wars character data using @sveltejs demo

Features:
- Fits in a tweet
- Handles loading state using the #await block
- Renders a _beautifully_ formatted JSON

Code in the next tweet πŸ‘‡
<script>
async function sw() {
let r = await fetch(
`swapi.co/api/people/${Math.ceil(Math.random() * 9) + 1}/`
);
return await r.json();
}
let p = sw();
</script>

{#await p}<p>Loading...</p>{:then char}<pre>{JSON.stringify(char, null, 2)}</pre>{/await}
Read 4 tweets
Get a Star Wars character with @sveltejs

<script>
function sw() {
return fetch(
`swapi.co/api/people/${Math.ceil(Math.random() * 10) + 1}`
).then(r => r.json());
}
</script>

{#await sw()}
<p>wait</p>
{:then c}
<p>{c.name}</p>
{/await}
If you'd like to learn how this works, I've just published an @eggheadio lesson which is FREE for the next couple of days! 😁

It's only 80 seconds and you can add "await block in @sveltejs" to your LinkedIn πŸ₯³

egghead.io/lessons/svelte…
Read 6 tweets

Related hashtags

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3.00/month or $30.00/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!