当前位置:网站首页>Methods and usage of promise async and await
Methods and usage of promise async and await
2022-06-30 11:10:00 【weifeng_ bushileng】
1.promise The process of changing a program from asynchronous execution to synchronous execution
For example, you can write a simple program to
<script type="text/javascript">
function one(){
return 'I am one'
}
function two(){
// Simulate a network request
setTimeout(()=>{
return 'I am two'
},3000)
}
function three(){
return 'I am three'
}
function run(){
console.log(one())
console.log(two())
console.log(three())
}
// Call run function
run()
</script>
here , The result of printing is determined by
I am one
I am two
I am three
Turned into
I am one
undefined
I am three
Because two There is a network request , But at this time, I do not output the results
Because there's only one run Method , Not until 3 Seconds later, I'll execute it again
To solve this problem , You need to turn asynchronous execution into synchronous execution
Need to change function two
function two(){
// It's a standard promise Method ,resolve Is a request returned successfully ,reject Is what the request fails to return
return new Promise((resolve,reject)=>{
// Here is also a setTimeout To simulate a network request
setTimeout(()=>{
resolve ('I am two‘’)
},3000)
})
}
The state at this time is 
Status at this time Because there is no statement , Must let two After performing To perform three
At this point we are going to run Change the status , Is to declare that there is promise Requested
async function run(){
console.log(one())
console.log( await two()) // This means waiting two After execution , To perform three
console.log(three())
}
His output is , display output
I am one
Wait for three seconds and output
I am two
I am three

边栏推荐
- 考研这些“不靠谱”的经验有多害人?
- Review of mathematical knowledge: curve integral of the second type
- 【leetcode 16】三数之和
- LiveData源码赏析三 —— 常见问题
- Qt之实现动效导航栏
- 林克庆到番禺区调研“发展要安全”工作 以“时时放心不下”责任感抓好安全发展各项工作
- 创建型-配置工厂
- Key library function based on Hal Library
- IDEA 又出新神器,一套代码适应多端!
- 100 important knowledge points that SQL must master: creating and manipulating tables
猜你喜欢

MySQL export SQL script file

Wireguard simple configuration

我们公司使用 7 年的这套通用解决方案,打通了几十个系统,稳的一批!

pytorch 筆記 torch.nn.BatchNorm1d

Deep dive kotlin synergy (16): Channel

Deep dive kotlin synergy (18): hot and cold data flow

Jetpack Compose DropdownMenu跟随手指点击位置显示

Matplotlib notes: contour & Contour

微信表情符号被写入判决书,你发的每个 emoji 都可能成为呈堂证供

The first China Digital Collection conference will be held soon
随机推荐
微信表情符号被写入判决书,你发的每个 emoji 都可能成为呈堂证供
LVGL 8.2 Simple Colorwheel
语音识别-基础(一):简介【语音转文本】
LVGL8.2 Simple Checkboxes
SQL必需掌握的100个重要知识点:使用视图
深潜Kotlin协程(十七):演员
SQL必需掌握的100个重要知识点:汇总数据
SQL必需掌握的100个重要知识点:使用子查询
100 important knowledge points that SQL must master: insert data
LVGL 8.2 Image styling and offset
The intelligent DNA molecular nano robot model is coming
China will force a unified charging interface. If Apple does not bow its head, iPhone will be kicked out of the Chinese market
LVGL 8.2图片缩放及旋转
[understanding of opportunity -34]: fate is within the light cone
Handler-源码分析
100 important knowledge points that SQL must master: using subquery
Unity Shader - 踩坑 - BRP 管线中的 depth texture 的精度问题(暂无解决方案,推荐换 URP)
The precision problem of depth texture in unity shader - stepping pit - BRP pipeline (there is no solution, it is recommended to replace URP)
Mysql database foundation: views and variables
promise async和await的方法与使用