当前位置:网站首页>Promise from introduction to mastery (Chapter 4 async and await)
Promise from introduction to mastery (Chapter 4 async and await)
2022-07-28 02:13:00 【Ruoshui CJJ】
Promise From entry to mastery ( The first 4 Chapter async and await)
The first 4 Chapter async and await
4.1. mdn file
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/async_function
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/await
4.2. async function
The return value of the function is promise object
promise The result of the object is determined by async The return value of the function execution determines ( and then Have the same characteristics )
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script> // async The return result of the function is Promise async function main(){
// 1. If the return value is a non Promise, So back Promise Status as fulfilled, The result value is the returned result // return "123"; // 2. If the return value is a Promise, So back Promise Status as Promise The state of , The result value is Promise The result returned // return new Promise((resolve,reject)=>{
// reject("123"); // }); // 3. If the return value is an exception thrown , So back Promise Status as reject, The result value is the exception thrown throw "error"; } let result = main() console.log(result); </script>
</body>
</html>
4.3 await function
4.3.1. await expression
await The expression on the right is usually promise object , But it can be something else
If the expression is promise object , await The return is promise The value of success
If the expression is another value , Take this value directly as await The return value of
**4.3.2 ** Be careful
await Must be written in async Function , but async There can be no await
If await Of promise failed , Will throw an exception , Need to pass through try…catch Capture processing
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script> // async The return result of the function is Promise async function main() {
let p = new Promise((resolve, reject) => {
resolve("ok"); }) // 1. If await The right side is Promise, So what's back is promise The value of success let res = await p; console.log(res); // 2. If await Right not Promise, Then return the data directly let res2 = await "123"; console.log(res2); // 3.1. If await The right side is Promise, but Promise Execution failure , that await It throws an exception , Need to use try-catch Capture let p2 = new Promise((resolve, reject) => {
reject("ok"); }); try {
let res3 = await p2; } catch (error) {
console.log(error); } } main() </script>
</body>
</html>
4.4 async and await Combined with cases
// It is required to read three files under the current folder , And merge output
// The callback function is implemented in the form of
fs.readFile('./1.html',(err, data1)=>{
if(err) throw err;
fs.readFile('./1.html',(err, data2)=>{
if(err) throw err;
fs.readFile('./1.html',(err, data3)=>{
if(err) throw err;
console.log(data1+data2+data3);
})
})
})
// async and await In combination with implementation
const fs = require('fs');
const util = require('util');
const minReadFile = util.promisify(fs.readFile);
async function main(){
try {
let data1 = await minReadFile("./1.html");
let data2 = await minReadFile("./2.html");
let data3 = await minReadFile("./3.html");
console.log(data1+data2+data3);
} catch (error) {
console.log(error);
}
}
4.5 async and await combination AJAX Send a request
<script>
function sendAJAX(url){
return new Promise((resolve, reject)=>{
})
}
let btn = document.querySelector("#btn");
btn.addEventListener('click', async function(){
let duanzi = await sendAJAX("https://api.apiopen.top/getJoke");
console.log(duanzi);
})
</script>
边栏推荐
- Gbase 8C transaction ID and snapshot (IV)
- Starfish OS X metabell strategic cooperation, metauniverse business ecosystem further
- WMS you don't know
- Traversal and properties of binary trees
- 都在说DevOps,你真正了解它吗?
- Redis design specification
- Appium click operation sorting
- 清除浮动的原因和六种方法(解决浮动飞起影响父元素和全局的问题)
- IT这个岗位,人才缺口百万,薪资水涨船高,上不封顶
- 软件测试面试题:为什要在一个团队中开展测试工作?
猜你喜欢

Skywalking distributed system application performance monitoring tool - medium

Go learning 01

Record a production deadlock

考研数学一元微分学证明题常见题型方法

Hcip 13th day notes

HyperMesh circular array - plug in
![[database data recovery] data recovery case of insufficient disk space of SQL Server database](/img/0e/908db40e1e8b7dd62e12558c1c6dc4.png)
[database data recovery] data recovery case of insufficient disk space of SQL Server database

萤石网络,难当「孤勇者」

Uniapp summary (applet)

Flex布局学习完成PC端
随机推荐
Flume(5个demo轻松入门)
JS what situations can't use json Parse, json.stringify deep copy and a better deep copy method
Promise从入门到精通(第4章 async 和 await)
小米网站主页面大模块——小模块+导航(浮动案例)
Execute add migration migration and report build failed
清除浮动的原因和六种方法(解决浮动飞起影响父元素和全局的问题)
BGP federal experiment
Software test interview question: please introduce the meaning of various test types in detail?
交叉熵原理及实现
结构伪类选择器—查找单个—查找多个—nth-of-type和伪元素
华为APP UI自动化测试岗面试真题,真实面试经历。
C# 使用Abp仓储访问数据库时报错记录集
Flex布局—固定定位+流式布局—主轴对齐—侧轴对齐—伸缩比
如何评估研发人员效能?软件工程师报告帮你看见每个人的贡献
Typescript中类的使用
Fiddler mobile packet capturing agent settings (for Huawei glory 60s)
synchronized详解
Gbase 8C recovery control function
数据输出-绘制动图
[website construction] update SSL certificate with acme.sh: change zerossl to letsencrypt