[Svelte 3] Use await block to wait for a promise and handle loading state in Svelte 3
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的更多相关文章
- 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 ...
- nodejs7.0 试用 async await
nodejs 7.0.0 已经支持使用 --harmony-async-await 选项来开启async 和 await功能. 在我看来,yield 和 async-await 都是在特定范围内实现了 ...
- 从C#到TypeScript - async await
总目录 从C#到TypeScript - 类型 从C#到TypeScript - 高级类型 从C#到TypeScript - 变量 从C#到TypeScript - 接口 从C#到TypeScript ...
- 不使用回调函数的ajax请求实现(async和await简化回调函数嵌套)
在常规的服务器端程序设计中, 比如说爬虫程序, 发送http请求的过程会使整个执行过程阻塞,直到http请求响应完成代码才会继续执行, 以php为例子 $url = "http://www. ...
- Promise,Async,await简介
Promise 对象 转载:http://wiki.jikexueyuan.com/project/es6/promise.html 基本用法 ES6 原生提供了 Promise 对象.所谓 Prom ...
- (译文)学习ES6非常棒的特性——Async / Await函数
try/catch 在使用Async/Await前,我们可能这样写: const main = (paramsA, paramsB, paramsC, done) => { funcA(para ...
- async/await,了解一下?
上一篇博客我们在现实使用和面试角度讲解了Promise(原文可参考<面向面试题和实际使用谈promise>),但是Promise 的方式虽然解决了 callback hell,但是这种方式 ...
- 已配置好的vue全家桶项目router,vuex,api,axios,vue-ls,async/await,less下载即使用
github 地址: https://github.com/liangfengbo/vue-cli-project 点击进入 vue-cli-project 已构建配置好的vuejs全家桶项目,统一管 ...
- Promise, Generator, async/await的渐进理解
作为前端开发者的伙伴们,肯定对Promise,Generator,async/await非常熟悉不过了.Promise绝对是烂记于心,而async/await却让使大伙们感觉到爽(原来异步可以这么简单 ...
随机推荐
- springcloud使用之服务的注册发现与消费
随着spring的发展我们发现spring提供了越来越多的项目来帮我们简化框架的搭建,使我们站在巨人的肩膀行走,让我们更关注于开发我们的逻辑.随着技术的更替,我们的新项目也逐渐使用了springboo ...
- mysql sql 分析
一.SQL 执行时间分析通过找到执行时间长的 SQL 语句,可以直观的发现数据层的效率问题. 1.通过 show processlist 来查看系统的执行情况mysql> show proces ...
- microk8s 搭建
一.简述 microk8s不通过虚拟机但与主机隔离方式,快速轻巧安装Kubernetes.通过在单个快照包中打包Kubernetes,Docker.io,iptables和CNI的所有上游二进制文件来 ...
- Java开发环境搭建(一)
一.JDK与JRE JDK:Java Development Kit,Java开发工具包,是给开发人员使用的,其中包含了Java的开发工具,如java.javac.jar等命令,同时也包含了JRE. ...
- HTML知识整理
以下是自己对以前所学的部分HTML相关知识进行的简单的梳理,水平有限,若有问题的地方,还请见谅. 1. 常用的浏览器及浏览器内核分别是什么? IE:Trident 内核 Firefox:gecko 内 ...
- mkimage命令
# mkimage Usage: mkimage -l image -l ==> list image header information mkimage [-x] -A arch -O os ...
- 隐藏Apache版本号及版本敏感信息
在安装软件前,我们需要隐藏软件的版本号及版本其他信息,这样就大大提高了安全指数. 只隐藏版本号: 我们在主配置文件里:httpd.conf [root@bqh- ~]# curl -i bbs.bqh ...
- 尚硅谷MySQL基础学习笔记
目录 写在前面 MySQL引入 数据库的好处 数据库的相关概念 数据库存储数据的特点 MySQL服务的启动和停止 MySQL服务端的登录和退出 MySQL的常用命令 MySQL语法规范 DQL(Dat ...
- NTP时间服务器+DHCP服务器的搭建
一.构建NTP时间服务器 ntp服务器监控端口UDP:123 安装ntp和ntpdate 命令:yum -y install ntp netdate 修改配置文件/etc/ntp.conf 启动nt ...
- Mongodb的主从复制
主从服务器的实现原理 首先,主节点会把本服务的与写有关的操作记录下来,读操来不记录,这些操作就记录在local数据库中的oplog.$admin这个集合中,这是一个固定集合,大小是可以配置的,主要是通 ...