当前位置:网站首页>The use of async (asynchronous) and await
The use of async (asynchronous) and await
2022-08-11 04:54:00 【qq_46302247】
正确使用方法:
<script>
let a = []
async function fun(){
console.log('开始喽!');
await interface()
console.log(a,'9999');
}
function interface() {
return new Promise((resolve, reject)=>{
setTimeout(()=>{
a = [1,9,22,6]
console.log('The interface data has been obtained!');
resolve()
},2000)
})
}
fun();
</script>
结果:
正常情况下,代码是同步执行的;例如,在函数A的内部调用函数B,Wait for the inner functionB执行完成后,才会执行函数Ain functionB后面的代码.
<script>
let a = []
function fun(){
console.log('开始喽!');
interface()
console.log(a,'9999');
}
function interface() {
for(let i=0; i<10;i++) {
console.log(i);
}
a = [1,9,22,6]
}
fun();
</script>
结果:
But when the functioninterfaceWhen the interface needs to be adjusted(我是用setTimeoutAnalog tuning interface),此时函数funThe code in is not executed synchronously,而是异步了
<script>
let a = []
function fun(){
console.log('开始喽!');
interface()
console.log(a,'9999');
}
function interface() {
setTimeout(()=>{
a = [1,9,22,6]
console.log('The interface data has been obtained!');
},2000)
}
fun();
</script>
结果:
边栏推荐
- ALSA音频架构 -- snd_pcm_open函数分析
- Events and scheduled tasks in Mysql
- 论文笔记:BBN: Bilateral-Branch Network with Cumulative Learningfor Long-Tailed Visual Recognition
- I wrote some code in my resume, but I still can't pass the interview
- Apache初体验
- 对象的创建以及显示转换
- 交换机和路由器技术-25-OSPF多区域配置
- Kong实现禁止国外IP访问
- Switches and routers technology - 24 - configure OSPF single area
- svg-icon的使用方法(svg-sprite-loader插件)
猜你喜欢
交换机和路由器技术-22/23-OSPF动态路由协议/链路状态同步过程
Network Skill Tree
0 Basic software test for career change, self-study for 3 months, 12k*13 salary offer
ERROR: Could not install packages due to an OSError: [Errno 2] No such file or directory: '/data/xxxx
网络技能树
To break the bottleneck of transactional work, the gentleman signs the electronic contract to release the "source power" of HR!
交换机和路由器技术-32-命名ACL
我的LaTeX入门
交换机和路由器技术-29-OSPF虚链路
论文笔记:BBN: Bilateral-Branch Network with Cumulative Learningfor Long-Tailed Visual Recognition
随机推荐
关于CC 攻击
async(异步)和await的使用
我的LaTeX入门
标识密码技术在 IMS 网络中的应用
Australia cyberspace security system construction
分层架构&SOA架构
[QNX Hypervisor 2.2用户手册]10.16 vdev virtio-blk
C语言:实用调试技巧
Embedded Sharing Collection 33
Kong实现禁止国外IP访问
力扣——青蛙跳台阶问题
Dry goods: The principle and practice of server network card group technology
Object Creation and Display Transformation
【FPGA教程案例50】控制案例2——基于FPGA的PD控制器verilog实现
智能指针笔记
澳大利亚网络空间安全体系建设论析
【ImageNet】数据集1000个类的名称
【yolov7系列三】实战从0构建训练自己的数据集
vector中resize() 用法排坑
Do you understand how the Selenium automated testing framework works?