当前位置:网站首页>promise async和await的方法与使用
promise async和await的方法与使用
2022-06-30 10:42:00 【weifeng_bushileng】
1.promise 将程序从异步的执行变成同步的一个过程
比如可以写一个简单的程序来
<script type="text/javascript">
function one(){
return 'I am one'
}
function two(){
//模拟一下网络请求
setTimeout(()=>{
return 'I am two'
},3000)
}
function three(){
return 'I am three'
}
function run(){
console.log(one())
console.log(two())
console.log(three())
}
//调用一下run函数
run()
</script>
此时,打印的结果又由
I am one
I am two
I am three
变成了
I am one
undefined
I am three
是因为two有一个网络请求,但是此时是我没有输出结果的
是因为只有一个run方法,不会等到3秒之后再去执行一次的
要解决这个问题,需要把异步的执行变成同步的
需要改变function two
function two(){
//这是一个标准的promise方法,resolve是一个请求成功返回的,reject是请求失败所要返回的
return new Promise((resolve,reject)=>{
//在这里也是用一个setTimeout去模拟一个网络请求
setTimeout(()=>{
resolve ('I am two‘’)
},3000)
})
}
此时的状态就是
此时的状态 是因为没有声明,要让two执行之后 再去执行three
此时我们要把 run改变一下状态,是要声明里面是有promise请求的
async function run(){
console.log(one())
console.log( await two()) //这个意思就是等two执行完了之后,再去执行three
console.log(three())
}
他的输出结果就是,显示输出
I am one
等三秒之后输出
I am two
I am three

边栏推荐
- [STL source code analysis] iterator
- Deep dive kotlin Xie Cheng (17): Actor
- 蚂蚁金服笔试题:需求文档有什么可以量化的【杭州多测师】【杭州多测师_王sir】...
- [rust weekly database] num bigint - large integer
- Anhui "requirements for design depth of Hefei fabricated building construction drawing review" was printed and distributed; Hebei Hengshui city adjusts the pre-sale license standard for prefabricated
- Viewing technological changes through Huawei Corps (V): smart Park
- 时间复杂度与空间复杂度
- Retest the cloud native database performance: polardb is still the strongest, while tdsql-c and gaussdb have little change
- 电商两位大佬花边新闻刷屏,代表电商回归正常,将有利于实体经济
- LeetCode Algorithm 86. Separate linked list
猜你喜欢

深潜Kotlin协程(十八):冷热数据流

Sarsa笔记

中国将强制统一充电接口,苹果如不低头,iPhone将被踢出中国市场

ESP32-C3入门教程 问题篇⑨——Core 0 panic‘ed (Load access fault). Exception was unhandled. vfprintf.c:1528

OceanBase 安装 yum 源配置错误及解决办法
![200000 bonus pool! [Alibaba security × ICDM 2022] the risk commodity inspection competition on the large-scale e-commerce map is in hot registration](/img/0e/19c4c97a582d7b2ad08ce806d7af03.jpg)
200000 bonus pool! [Alibaba security × ICDM 2022] the risk commodity inspection competition on the large-scale e-commerce map is in hot registration

【STL源码剖析】容器(待补充)

Cp2112 teaching example of using USB to IIC communication

Unity Shader - 踩坑 - BRP 管线中的 depth texture 的精度问题(暂无解决方案,推荐换 URP)

What is erdma as illustrated by Coptic cartoon?
随机推荐
基于HAL库的LED驱动库
Kotlin 协程调度切换线程是时候解开谜团了
再测云原生数据库性能:PolarDB依旧最强,TDSQL-C、GaussDB变化不大
[rust weekly database] num bigint - large integer
LVGL 8.2 Simple Image button
时间复杂度与空间复杂度
LVGL 8.2 Drop down in four directions
The intelligent DNA molecular nano robot model is coming
pytorch 笔记:validation ,model.eval V.S torch.no_grad
从开源项目探讨“FPGA挖矿”的本质
sCrypt 中的 ECDSA 签名验证
LED driver library based on Hal Library
The first China Digital Collection conference will be held soon
[proteus simulation] Arduino uno led simulated traffic light
内存逃逸分析
小程序中读取腾讯文档的表格数据
MySQL export SQL script file
AMS源码解析
运动App如何实现端侧后台保活,让运动记录更完整?
LVGL 8.2 Image styling and offset