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. 1.JVM前奏篇(看官网怎么说)

    JVM(Java Virtual Machine) 前奏篇(看官网规范怎么说) 1.The relation of JDK/JRE/JVM 在下图中,我们所接触的,最熟悉,也是经常打交道的 最顶层 J ...

  2. kubernetes1.16 配置 metrics-server

    kubernetes1.16 版本对应最新的metrics-server为v0.3.5. 下载metrics-server配置文件 git clone https://github.com/kuber ...

  3. T-SQL行列相互转换命令:PIVOT和UNPIVOT使用详解

    最近在维护一个ERP 做二次开发 ,在查找数据源的时候看到前辈写的SQL ,自己能力有限 ,就在网上找找有关这几个关键字的使用方法.做出随笔以做学习之用 T-SQL语句中,PIVOT命令可以实现数据表 ...

  4. GC收集器

    新生代收集器 Serial New 单线程收集器,工作时必须暂停其他线程: 简单高效,没有线程交互开销: 基于复制算法: Parallel New 对Serial的改进,多线程: CPU数量<4 ...

  5. 小程序的数据监听 用法和vue中的watch一样====使用通配符监听所有自数据字段的变化

    使用通配符监听所有自数据字段的变化

  6. Mybatis @Many注解一对多关联映射

    @Many注解:fetchType属性用于配置是否延迟加载

  7. UOJ343 清华集训2017 避难所 构造、打表

    传送门 玄学题 考虑构造三个数\(p_1p_2,p_1p_2,p_1p_2\)满足贪心分解会分解为\(p_1^3,p_2,p_2,p_2\),那么需要满足条件 1.\(p_1 , p_2 \in Pr ...

  8. C#使用Linq to XML进行XPath查询

    最近在用到HtmlAgliltyPack进行结点查询时,发现这里选择结点使用的是XPath.所以这里总结一下在C#中使用XPath查询XML的方法.习惯了用Linq,这里也是用的Linq to xml ...

  9. vue 生命周期的详解

    一.vue生命周期的解析 > 1>什么是vue生命周期 每个vue实例在被创建之前都要经过一系列的初始化过程,这个过程就是vue的生命周期.详细来说,就是Vue实例从开始创建,初始化数据, ...

  10. Git创建工作目录与常用指令

    1.创建工作目录与常用指令 工作目录(WorkSpace)一般就是你希望Git帮助你管理的文件夹,可以是你项目的目录,也可以是一个空目录,建议不要有中文. 日常使用只要记住下图6个命令: 2.提交管理 ...