当前位置:网站首页>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>
结果:
边栏推荐
猜你喜欢
findViewById返回null的问题
C语言:实用调试技巧
自研能力再获认可,腾讯云数据库入选 Forrester Translytical 报告
Jetson Orin平台4-16路 GMSL2/GSML1相机采集套件推荐
JVM 垃圾回收的概述与机制
IP-Guard如何禁止运行U盘程序
交换机和路由器技术-34-动态NAT
To break the bottleneck of transactional work, the gentleman signs the electronic contract to release the "source power" of HR!
C语言题解:谁是凶手!
延长经济保险(jeecgboot)
随机推荐
交换机和路由器技术-32-命名ACL
自研能力再获认可,腾讯云数据库入选 Forrester Translytical 报告
如何将360全景图导出高清短视频分享到视频平台上?
form表单提交数据库中文变成问号
【服务器安装mysql】centos7下使用mysql离线安装包安装mysql5.7
[QNX Hypervisor 2.2用户手册]10.16 vdev virtio-blk
MQ框架应用比较
1815. Get the maximum number of groups of fresh donuts state compression
CAD2020 打开错误报告 e06d7363h Exception at 13644F69h
Selenium自动化测试框架工作原理你明白了吗?
leetcode 9. 回文数
Optimization is a kind of habit low starting point is the "standing near the critical"
洛谷P4560 Wall 砖墙
How to read a paper
二叉堆的基础~
FPGA工程师面试试题集锦121~130
交换机和路由器技术-29-OSPF虚链路
svg-icon的使用方法(svg-sprite-loader插件)
无线电射频能量的收集
【实战场景】商城-折扣活动设计方案