[Javascript] Await a JavaScript Promise in an async Function with the await Operator
The await operator is used to wait for a promise to settle. It pauses the execution of an async function until the promise is either fulfilled or rejected.
const API_URL = "https://starwars.egghead.training/";
const output = document.getElementById("output");
const spinner = document.getElementById("spinner");
async function queryAPI(endpoint) {
const response = await fetch(API_URL + endpoint);
if (response.ok) {
return response.json();
}
throw Error("Unsuccessful response");
}
async function main() {
try {
const [films, planets, species] = await Promise.all([
queryAPI("films"),
queryAPI("planets"),
queryAPI("species")
]);
output.innerText =
`${films.length} films, ` +
`${planets.length} planets, ` +
`${species.length} species`;
} catch (error) {
console.warn(error);
output.innerText = ":(";
} finally {
spinner.remove();
}
}
main();
[Javascript] Await a JavaScript Promise in an async Function with the await Operator的更多相关文章
- 关于async function(){ let res = await } 详解
本文引自: https://www.jianshu.com/p/435a8b8cc7d3 async function fn(){ //表示异步,这个函数里面有异步任务 let result = aw ...
- Promise嵌套问题/async await执行顺序
/* 原则: 执行完当前promise, 会把紧挨着的then放入microtask队尾, 链后面的第二个then暂不处理分析, */ 一. new Promise((resolve, reject) ...
- javascript ES6 新特性之 Promise,ES7 async / await
es6 一经推出,Promise 就一直被大家所关注.那么,为什么 Promise 会被大家这样关注呢?答案很简单,Promise 优化了回调函数的用法,让原本需要纵向一层一层嵌套的回调函数实现了横向 ...
- Javascript 使用 async 声明符和 await 操作符进行异步操作
async function 声明用于定义一个返回 AsyncFunction 对象的异步函数 await 操作符用于等待一个Promise 对象.它只能在异步函数 async function 中 ...
- Javascript异步编程之三Promise: 像堆积木一样组织你的异步流程
这篇有点长,不过干货挺多,既分析promise的原理,也包含一些最佳实践,亮点在最后:) 还记得上一节讲回调函数的时候,第一件事就提到了异步函数不能用return返回值,其原因就是在return语句执 ...
- JavaScript:学习笔记(9)——Promise对象
JavaScript:学习笔记(9)——Promise对象 引入Promise Primose是异步编程的一种解决方案,比传统的解决方案回调函数和事件更加合理和强大.如下面为基于回调函数的Ajax操作 ...
- 答应我,这次必须搞懂!痛点难点Promise。(小点心async/await,基于Promise的更优方案)
Promise 出现的原因 在 Promise 出现以前,我们处理一个异步网络请求,大概是这样: // 请求 代表 一个异步网络调用. // 请求结果 代表网络请求的响应. 请求1(function( ...
- Promise对象和async函数
Promise对象 //1开始 function fna(){ console.log('1开始'); var p = new Promise(function(resolve, reject){ / ...
- JavaScript权威设计--JavaScript函数(简要学习笔记十一)
1.函数调用的四种方式 第三种:构造函数调用 如果构造函数调用在圆括号内包含一组实参列表,先计算这些实参表达式,然后传入函数内.这和函数调用和方法调用是一致的.但如果构造函数没有形参,JavaScri ...
随机推荐
- dotnet core 运行程序注意事项
.net core 程序 debug版本无法直接运行,因为没有相关的dll,只有在进入项目文件夹那一层,执行 dotnet run 来运行,会自动链接 当前用户的个人文件夹下的 .nuget/.pac ...
- luogu2158 [SDOI2008]仪仗队 欧拉函数
点 $ (i,j) $ 会看不见当有 $ k|i $ 且 $ k|j$ 时. 然后就成了求欧拉函数了. #include <iostream> #include <cstring&g ...
- python - 接口自动化测试实战 - case1 - 优化版
题目: 基于以下两个接口和数据完成接口自动化测试,并生成测试报告: '''登录 login='http://47.107.168.87:8080/futureloan/mvc/api/member/l ...
- Linux 安装 tree命令
通过yum在线安装tree包 yum install tree -y
- ICCID
ICCID:Integrate circuit card identity 集成电路卡识别码(固化在手机SIM卡中) ICCID为IC卡的唯一识别号码,共有20位数字+英文组成,其编码格式为:XXX ...
- 当网络中断的时候,JTA全局事务管理,究竟会不会回滚???
前言:有人问了我一个问题,就是说在网络中断的时候,JTA的全局事务管理,会不会回滚?当时说会回滚,但没给对方说清楚理由,也不太认同我的观点.现在总结一下. 今天一天都在看文档(也查了一些博客和网站), ...
- 动态方式破解apk进阶篇(IDA调试so源码)
动态方式破解apk进阶篇(IDA调试so源码) 来源 https://blog.csdn.net/qq_21051503/article/details/74907449 下面就说关于在IDA中And ...
- BZOJ-1798 维护序列
线段树.支持区间加.区间乘.区间查询和. 标记下移还有取模要注意. var n,p,q,i,s,t:longint; a:int64; num,n1,n2,n3:array[0..500000] of ...
- MySQL-MongoDB开源监控利器之PMM
背景说明: PMM是percona公司提供的一个对于MySQL和MongoDB的监控和管理平台.PMM有两部分组成PMM Client和PMM Server PMM Client:安装在每一台需要进行 ...
- 使用Matlab实现对图片的缩放
在做图像处理的时候,有时需要对图片的像素进行放大或则缩小. 使用Matlab很容易实现对图像的放大和缩小.这里只讲缩放到固定像素的方法. clear; clc; %清除以前的数据 folderName ...