当前位置:网站首页>async和await用法介绍
async和await用法介绍
2022-08-01 23:46:00 【web18334137065】
一;async
1.函数前面加上 async 关键字,则该函数会返回一个结果为 promise 的对象。
2. async 函数返回 promise 对象的状态。
2.1:如果返回的是一个非 Promise 类型的数据, 则async 函数返回 promise 的状态 为 fulfilled 成功。
2.2:如果返回的是一个 Promise对象,则 async 函数返回 promise 的状态由返回的Promise对象的状态决定。
2.3:如果 throw Errow 抛出异常,则 async 函数返回 promise 的状态为 rejected 失败。

二;await
1.await 右侧的表达式一般为 promise 对象。
2.await 是在等一个表达式的结果,等一个返回值(回调成功的内容)
3.如果 await 右侧是一个 promise 对象,则 await 返回的是 promise 成功的值。
注意:
1.await 必须写在 async 函数中,但 async 函数中可以没有 await。
2.如果 await 的 promise 失败了,就会抛出异常,该异常需要通过 try catch 捕获处理。
3.如果它等到的是一个 Promise 对象,await 就忙起来了,它会阻塞后面的代码,等着 Promise 对象 resolve,然后得到 resolve 的值,作为 await 表达式的运算结果。
async function main(){
let p = new Promise((resolve,reject)=>{
resolve('ok')
})
try {
// 如果 await 后面的 promise是成功的,则返回成功的内容
let res = await p
console.log(res);
}
// 如果 await 右侧的 promise状态为错误,则需要通过catch捕获错误信息
catch (error) {
// error 接收的是 promise错误的返回值
console.log(error);
}
}
main()
三;async 和 await 结合发送 ajax
1.封装一个 发送GET请求的 promise
function sendAjax(url) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest()
xhr.open('GET', url)
xhr.send()
// 处理返回结果
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
// 判断状态码
if (xhr.readyState >= 200 && xhr.readyState < 300) {
resolve(xhr.response)
}else {
reject(xhr.status)
}
}
}
})
}
2.async和await结合发送ajax请求
// 触发点击事件发送ajax请求
let btn = document.querySelector('#btn')
btn.addEventListener('click', async function () {
try {
let res = await sendAjax('get请求地址')
console.log(res);
} catch (error) {
console.log(error);
}
})
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- 访问控制台中的选定节点
- Several interview questions about golang concurrency
- 邻接表与邻接矩阵
- 机器学习文本分类
- @Transactional 注解使用详解
- Solve the port to take up
- numpy.around
- 斜堆、、、
- problem solved
- With a monthly salary of 12K, the butterfly changed to a new one and moved forward bravely - she doubled her monthly salary through the career change test~
猜你喜欢

分享一份接口测试项目(非常值得练手)

@WebServlet注解(Servlet注解)

云原生DevOps环境搭建

Secondary Vocational Network Security Competition B7 Competition Deployment Process

chrome copies the base64 data of an image

请问什么是 CICD

Share an interface test project (very worth practicing)
![[C language advanced] file operation (2)](/img/4d/49d9603aeed16f1600d69179477eb3.png)
[C language advanced] file operation (2)

What is CICD excuse me

【MySQL篇】初识数据库
随机推荐
How do programmers solve online problems gracefully?
UML diagram of soft skills
@Scheduled注解详解
软件测试之移动APP安全测试简析,北京第三方软件检测机构分享
Avoid hidden text when loading fonts
Architecture basic concept and nature of architecture
递归:方法调用自身
【MySQL系列】 MySQL表的增删改查(进阶)
Docker实践经验:Docker 上部署 mysql8 主从复制
Programmer is still short of objects? A new one is enough
Use Jenkins for continuous integration, this knowledge point must be mastered
6134. 找到离给定两个节点最近的节点-力扣双百代码
A brief analysis of mobile APP security testing in software testing, shared by a third-party software testing agency in Beijing
6134. Find the closest node to the given two nodes - force double hundred code
yay 报错 response decoding failed: invalid character ‘<‘ looking for beginning of value;
在CDH的hue上的oozie出现,提交 Coordinator My Schedule 时出错
@WebServlet注解(Servlet注解)
Quartus uses tcl files to quickly configure pins
【MySQL篇】初识数据库
Flink Yarn Per Job - 提交流程一