[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却让使大伙们感觉到爽(原来异步可以这么简单 ...
随机推荐
- 47 容器(六)——HashMap
HashMap的概念 HashMap底层实现了哈希表,这是一种非常重要的数据结构,对于以后我们理解很多技术都有帮助,例如 redis数据库的核心技术和HashMap一样,因此,非常有必要让大家理解. ...
- D03-R语言基础学习
R语言基础学习——D03 20190423内容纲要: 1.导入数据 (1)从键盘输入 (2)从文本文件导入 (3)从excel文件导入 2.用户自定义函数 3.R访问MySQL数据库 (1)安装R ...
- 安装docker的shell脚本
docker_install.sh #!/bin/bash # author:qiao # 安装并启动docker # 使用阿里云镜像 安装社区版 # 卸载旧的版本 sudo yum remove d ...
- Spring Cloud Zuul源码
一.Zuul源码分析(初始化流程.请求处理流程)
- Spring Cloud Alibaba学习笔记(19) - Spring Cloud Gateway 自定义过滤器工厂
在前文中,我们介绍了Spring Cloud Gateway内置了一系列的内置过滤器工厂,若Spring Cloud Gateway内置的过滤器工厂无法满足我们的业务需求,那么此时就需要自定义自己的过 ...
- Java 枚举和抽象类添加状态值
枚举: public enum CourseTypeEnum { VIDEO_COURSE(1,"录制课程"), LIVE_COURSE(2,"直播课程"), ...
- JPA、Hibernate、Spring data jpa之间的关系,以及和springboot的整合
什么么是JPA? 全称Java Persistence API,可以通过注解或者XML描述[对象-关系表]之间的映射关系,并将实体对象持久化到数据库中. 为我们提供了: 1)ORM映射元数据:JPA支 ...
- extend Thread 和 implements Runnable
原文地址:extend Thread 和 implements Runnable 一个Thread的实例只能产生一个线程 or: 同一实例(Runnable实例)的多个线程 look: public ...
- .Net Core 注入学习——注册服务
解析 .Net Core 注入——注册服务发表于:2017-10-23 10:47 作者:行走即歌 来源:51Testing软件测试网采编字体:大 中 小 | 上一篇 | 下一篇 |我要投稿 | 推荐 ...
- 2 Match、Filter、排序、分页、全文检索、短语匹配、关键词高亮
查索引内所有文档记录 GET /beauties/my/_search GET /beauties/my/_search { "query":{ & ...