当前位置:网站首页>Async function and await expression in ES6
Async function and await expression in ES6
2022-07-22 14:13:00 【Bald mule】
Catalog
3、 ... and 、async await ajax Use
One 、async function
1. Definition
async It's a modifier ,async The defined function will return a by default Promise object resolve Value , So right. async Functions can be used directly then operation , The value returned is then Method .
2. demonstration
async function demo(){
// 1: When the return value is not promise object When the function is called, it is the successful value .
// return "succ";
// 2: When it comes back to promise object So the function (promise object ) Results of and returned promise In the same state .
return new Promise((resolve,reject)=>{
let flag = true;
if(flag){
resolve("succ");
}else{
reject("error");
}
})
}
const MyPromise = demo();
MyPromise.then((resolve)=>{
console.log(resolve);
},(reject)=>{
console.log(reject);
})
Two 、await expression
1. Definition
It's also a modifier ,await keyword Only on the async Internal function , await The role of keywords Is to get Promise Content returned in , What you get is Promise Function resolve perhaps reject Value .
2. Be careful
1:await Must be on async Function .
2:await The expression on the right is usually promise object .
3:await You can return to the right side promise The value of success .
4:await On the right side of the promise If it fails , Will throw an exception , Need to pass through try…catch Capture processing .
3. demonstration
(1) success
// 1:await It needs to be written in async Inside the function .
// 2:await Embellished Promise The value returned is resolve Value .
// 3: The following code needs to wait await After the results of the .
async function demo(){
const a = await "a";
const b = await new Promise((resolve,reject)=>{
setTimeout(()=>{
console.log(" The timer executed ....");
resolve("b");
},3000);
});
const c = await "c";
console.log(a,b,c);
}
demo();
(2) Failure : Failed code await Wrong code Need to use try catch Capture
async function demo(){
try{
const a = await new Promise((relsolve,reject)=>{
reject(" The data doesn't exist ");
})
}catch(error){
console.log(error);
}
}
demo();
3、 ... and 、async await ajax Use
First create the object , use get The method of requests the following incoming address , Resend request , By judging whether there is data output .
function sendajax(url){
return new Promise((resolve,reject)=>{
const http = new XMLHttpRequest();// Create objects
http.open("GET",url);// use get Method request address
http.send();// Send a request
http.onreadystatechange = function(){
if(http.readyState==4){
if(http.status==200){
resolve(JSON.parse(http.response));
}
}
}
})
}
async function demo(){
const res = await sendajax("http://127.0.0.1:8848/web2209/ES6/test.json");
if(res.code==200){
console.log(" There's data ");
}else{
console.log(" No data ");
}
}
demo();
边栏推荐
猜你喜欢
随机推荐
9.zabbix-SNMP监控
bom操作——缓动动画
微服务高可用的两个关键技巧,你一定用得上
AQS of concurrent memory model -- reentrantlock locking process
Pads Sketchpad box
Is it safe for Huatai Securities to open an account and open online banking? Do you need to go to the sales department
8.zabbix分布式
9. ZABBIX SNMP monitoring
Looking for Fibonacci number
解决 error: Your local changes to the following files would be overwritten by merge:XXXX
Raspberry pie - Cloud Server Deployment - intranet penetration - use of cpolar tool
6. ZABBIX alarm setting
MySQL高级篇(C)
[take you to the cloud native system] Part 4: kubernetes from entry to mastery
微服务架构 | 服务注册发现中心/配置中心/消息总线 - [nacos] TBC...
There are two key skills for high availability of microservices, which you must use
C language hierarchical understanding (C language branches and circular statements)
7. ZABBIX auto discovery automatic logon
JUC thread pool
6.zabbix报警设置