[Javascript] Await a JavaScript Promise in an async Function with the await Operator
JavaScriptasync in The with an Function Promise
2023-09-14 09:00:49 时间
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 基础_JavaScript高级编程
- 关于JavaScript网页计时器
- Javascript的private属性
- 如何使用JavaScript来对表单中的电话号码做验详解编程语言
- 分享几道经典的javascript面试题详解编程语言
- javascript入门·动态的时钟,显示完整的一些方法,新年倒计时
- JavaScript开发时的五个注意事项
- javascript解析后的xml对象的读取方法细解
- JavaScript设计模式富有表现力的Javascript(一)
- Javascript异步加载详解(浏览器在javascript的加载方式)
- JavaScript高级程序设计阅读笔记(十六)javascript检测浏览器和操作系统-detect.js
- javascript中验证大写字母、数字和中文
- JavaScript日期时间格式化函数分享
- JavaScript中伪协议javascript:使用探讨
- Javascript基础教程之JavaScript语法
- JavaScript及jquey实现多个数组的合并操作