[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却让使大伙们感觉到爽(原来异步可以这么简单 ...
随机推荐
- 二次剩余定理及Cipolla算法入门到自闭
二次剩余定义: 在维基百科中,是这样说的:如果q等于一个数的平方模 n,则q为模 n 意义下的二次剩余.例如:x2≡n(mod p).否则,则q为模n意义下的二次非剩余. Cipolla算法:一个解决 ...
- 【flask】登陆后返回之前重定向跳转的页面
登陆后返回之前重定向跳转的页面 一.前言 实现强制跳转到登陆页面,登陆后返回之前的页面的功能.网上跳登陆页面的很多:返回之前页面功能没多少.这里我只是用了自己的方法,有缺点和其他方法也请指点!(´ε` ...
- mysql疑问
- condition的使用
condition 的作用:条件锁 需求: 按需执行三个线程. 用wait,notify的方式: /** * 有序线程 wait,notify版 */ public class OrderThread ...
- python 操作redis集群
一.连接redis集群 python的redis库是不支持集群操作的,推荐库:redis-py-cluster,一直在维护.还有一个rediscluster库,看GitHub上已经很久没更新了. 安装 ...
- 2.9_Database Interface ADO结构组成及连接方式实例
说通俗点OLE DB和ODBC都是最底层的东西,而ADO对象给我们提供了一个“可视化”和应用层直接交互的组件,ADO对象T通过OLE DB间接取得数据库中的数据,如下图: 从上面看出,可以说ADO是应 ...
- C# vb .net实现扭曲角特效滤镜图像处理
在.net中,如何简单快捷地实现Photoshop滤镜组中的扭曲角效果呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 第一 ...
- android studio 出现 Default Activity not found
1.AndroidManifest.xml <activity android:name=".activity.StartPage" android:screenOrient ...
- iview表单数字验证
sort: [ {required: true, message: '请填写栏目排序', trigger: 'blur'}, {type: 'number', message: '请输入数字', tr ...
- 尚硅谷韩顺平Linux教程学习笔记
目录 尚硅谷韩顺平Linux教程学习笔记 写在前面 虚拟机 Linux目录结构 远程登录Linux系统 vi和vim编辑器 关机.重启和用户登录注销 用户管理 实用指令 组管理和权限管理 定时任务调度 ...