当前位置:网站首页>Async/await for ES6
Async/await for ES6
2022-06-12 12:33:00 【I'm Mr.C】
Async/Await The role of
Async It means : asynchronous
Await It means : wait for
stay ES6 in , Use async/await It means : Wait for the asynchrony to complete before continuing code execution .JS in , The execution code runs on the main thread first , When encountering asynchrony , Asynchronously goes to the message queue , Wait until the main thread finishes running before executing the asynchronous task of the message queue .
Async/Await Use
Async/Await The use of is not complicated .
async function a(){
await ···// Asynchronous code
}
This is it. async/await Use . Although it's very convenient to use , But there are many things to pay attention to .
Async/Await It's used together , Use await You have to add async Embellish , And used async Can not be applied in the function of await
async The return value of the function will be encapsulated as Promise object
async function a(){
return "a"
}
console.log(a());
Output at console :
It is Promise object , We can use then To get the results
async function a(){
return "a"
}
a().then((item)=>{
console.log(item);
});
// Console output a
// Note that the function uses return Return value ,return The value of is in then Function
// If you want to make the value between catch Function , should return One promise.reject();
// for example :
//async function a(){
// return Promise.resolve("a1"); Here with return equally , The value will be in then In the callback
// return Promise.reject("a2"); The value is in catch Calling back
//}
await Wait for the asynchronous execution to complete
await It should be noted that it is necessary to combine Promise Use it together
//async function a(){
// await Execute asynchronous code
//}
// First contact Async/Await It's easy to get into the misunderstanding when you're in the middle of the world :await With no Promise Use it together
// The wrong way to write
//(async function a(){
// await setTimeout(()=>{
// console.log(1);
// },3000);
// console.log(2);
//})();
// First contact await The output will be considered as 1 Until then 2, It's actually wrong . Console or output 2 Then the output 1, The reason is that await With no Promise Use it together
(async function a(){
await new Promise((resolve,reject)=>{
setTimeout(()=>{
resolve(1);
},1000);
}).then((item)=>{
console.log(item);
});
console.log(2);
})()
// Console output 1 Then the output 2
边栏推荐
- 传统的DOM渲染方式?
- InfluxDB2.x 基准测试工具 - influxdb-comparisons
- sublime_text使用
- The advantages of saving pointers when saving objects with vector and the use of reserve
- Dom and BOM in JS
- 深度剖析指针的进阶——C语言的进阶篇
- A short guide to SSH port forwarding
- Click to generate 4-bit random number and verify code setting
- 一个ES设置操作引发的“血案”
- Shielding does not display vs warning
猜你喜欢

时序数据库 - InfluxDB2 docker 安装

Time series database - incluxdb2 docker installation

SWI-Prolog的下载与使用

点云配准--gicp原理与其在pcl中的使用

Lightweight ---project
![[C language] keyword static & Multi file & guessing game](/img/82/fa3556522531f83e95938fc4efbf1e.png)
[C language] keyword static & Multi file & guessing game

Promise knowledge

JS built in object

VGg small convolution instead of large convolution vs deep separable convolution

VS2019 设置 CTRL+/ 为注释和取消注释快捷键
随机推荐
Boot entry directory
[translation] go references - the go memory model | memory model for Chinese translation of official golang documents
Stress - system pressure simulation tool
A. Prefix range
Bank layout meta universe: digital collections, digital employees become the main track!
object. Defineproperty basic usage
你不会只会用console.log()吧?
Quantization and Training of Neural Networks for Efficient Integer-Arithmetic-Only Inference
点云配准--gicp原理与其在pcl中的使用
Preliminary study on Regional Simulation of crop model
Conversion between ROS map picture pixels and map coordinate system coordinates
Performance comparison test of channel and condition variables of golang in single production and single consumption scenarios
JS built in object
用cloneNode 克隆,解决id问题/方法 深复制和浅复制修改id的方法
C语言进阶篇——万字详解指针和qsort函数
In navigation, the solution of adding borders to affect the layout
[转]placement new
Problems encountered in generating MP3 from text to speech through iFLYTEK voice API
【您编码,我修复】WhiteSource正式更名为Mend
MySQL review