Most web applications have to deal with asynchronous data at some point.

Svelte 3 apps are no different, luckily Svelte allows us to await the value of a promise directly in markup using await block.

In this lesson we're going to learn how to use the await block to fetch the data from a Star Wars API and both display the data and handle loading state.

// Before:

<script>
async function getRandomStarWarsCharacter() {
const randomNumber = Math.floor(Math.random() * 10) + 1;
const apiResponse = await fetch(
`https://swapi.co/api/people/${randomNumber}/`
); return await apiResponse.json();
} let character;
getRandomStarWarsCharacter().then(value => (character = value));
</script> <h1>{!character ? 'Loading ...' : character.name}</h1>
// After:
<script>
async function getRandomStarWarsCharacter() {
const randomNumber = Math.floor(Math.random() * 10) + 1;
const apiResponse = await fetch(
`https://swapi.co/api/people/${randomNumber}/`
); return await apiResponse.json();
} let promise = getRandomStarWarsCharacter();
</script> <!-- <h1>{!character ? 'Loading ...' : character.name}</h1> --> {#await promise}
<h1>Loading...</h1>
{:then character}
<h1>{character.name}</h1>
{/await}

[Svelte 3] Use await block to wait for a promise and handle loading state in Svelte 3的更多相关文章

  1. Async Performance: Understanding the Costs of Async and Await

    Stephen Toub Download the Code Sample Asynchronous programming has long been the realm of only the m ...

  2. nodejs7.0 试用 async await

    nodejs 7.0.0 已经支持使用 --harmony-async-await 选项来开启async 和 await功能. 在我看来,yield 和 async-await 都是在特定范围内实现了 ...

  3. 从C#到TypeScript - async await

    总目录 从C#到TypeScript - 类型 从C#到TypeScript - 高级类型 从C#到TypeScript - 变量 从C#到TypeScript - 接口 从C#到TypeScript ...

  4. 不使用回调函数的ajax请求实现(async和await简化回调函数嵌套)

    在常规的服务器端程序设计中, 比如说爬虫程序, 发送http请求的过程会使整个执行过程阻塞,直到http请求响应完成代码才会继续执行, 以php为例子 $url = "http://www. ...

  5. Promise,Async,await简介

    Promise 对象 转载:http://wiki.jikexueyuan.com/project/es6/promise.html 基本用法 ES6 原生提供了 Promise 对象.所谓 Prom ...

  6. (译文)学习ES6非常棒的特性——Async / Await函数

    try/catch 在使用Async/Await前,我们可能这样写: const main = (paramsA, paramsB, paramsC, done) => { funcA(para ...

  7. async/await,了解一下?

    上一篇博客我们在现实使用和面试角度讲解了Promise(原文可参考<面向面试题和实际使用谈promise>),但是Promise 的方式虽然解决了 callback hell,但是这种方式 ...

  8. 已配置好的vue全家桶项目router,vuex,api,axios,vue-ls,async/await,less下载即使用

    github 地址: https://github.com/liangfengbo/vue-cli-project 点击进入 vue-cli-project 已构建配置好的vuejs全家桶项目,统一管 ...

  9. Promise, Generator, async/await的渐进理解

    作为前端开发者的伙伴们,肯定对Promise,Generator,async/await非常熟悉不过了.Promise绝对是烂记于心,而async/await却让使大伙们感觉到爽(原来异步可以这么简单 ...

随机推荐

  1. win10无法安装软件解决

    https://www.windowscentral.com/how-fix-network-resource-unavailable-install-error-windows-10

  2. C程序设计语言练习 第二章

    2.3 常量 strlen函数:返回s的长度 int strlenn(char s[]) { int i=0; while(s[i] != '\0') ++i; return i; } 2.7 类型转 ...

  3. c语言中typeof关键字

    为什么因为一个关键字而专门写一篇随笔呢?因为这个关键字在Linux里面地位非同一般!这个关键字typeof在linux内核里面使用非常广泛! 下面这两个等效声明,用于声明int类弄的变量atypeof ...

  4. 在 WPF 中获取一个依赖对象的所有依赖项属性

    原文:在 WPF 中获取一个依赖对象的所有依赖项属性 本文介绍如何在 WPF 中获取一个依赖对象的所有依赖项属性. 本文内容 通过 WPF 标记获取 通过设计器专用方法获取 通过 WPF 标记获取 p ...

  5. C盘清理、C盘瘦身、省出30G

    三招C盘瘦身30G,清理win10系统中虚占C盘空间的三大祸害 1.对C盘进行“磁盘清理” C盘右键->属性->磁盘清理->清理系统文件->勾选“windows更新清理”-&g ...

  6. Description Resource Path Location Type Unknown Unknown Unknown org.eclipse.core.internal.resources.Marker is not of a displayable type

    是访问限制报错. 方法一: 全局属性Project>preferences>java>Compiler>Errors/Warnings>把右侧的[Deprecated a ...

  7. 【洛谷 P2051】 [AHOI2009]中国象棋(DP)

    题目链接 首先想到状压dp,但是\(n,m\)高达100,怎么压? 容易发现,每行每列最多两个象棋,否则就直接gg了. 一个巧妙的设置状态的方式是,只需要记录到当前行有多少列是放了1个炮和2个炮. 然 ...

  8. native function 'Window_sendPlatformMessage' (4 arguments) cannot be found

    https://github.com/pauldemarco/flutter_blue/issues/140 https://github.com/flutter/flutter/issues/168 ...

  9. viewer与 iview Carousel(走马灯) 结合使用,图片无法显示

    问题原因:iview Carousel (走马灯)在加载是如果没有图片 它没有高度.之后给src赋值,图片无法显示,拖动一下浏览器宽或搞它就会显示. 走马灯解决这个问题方法:1在Carousel或其父 ...

  10. springboot使用过滤器Filter

    先创建过滤器配置类: ## 引入的包部分省略... @Configuration public class FilterConfig { @SuppressWarnings({"rawtyp ...