当前位置:网站首页>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>
结果:
边栏推荐
猜你喜欢
随机推荐
智能指针笔记
C语言题解:谁是凶手!
自研能力再获认可,腾讯云数据库入选 Forrester Translytical 报告
交换机和路由器技术-31-扩展ACL
CAD2020 打开错误报告 e06d7363h Exception at 13644F69h
Redis: Solve the problem of modifying the same key with distributed high concurrency
在 关闭页面/卸载(unload)文档 之前向服务器发送请求
The priority queue
交换机和路由器技术-25-OSPF多区域配置
Word2021 中的图片保存后就变模糊了
对象的创建以及显示转换
FPGA工程师面试试题集锦111~120
[QNX Hypervisor 2.2用户手册]10.16 vdev virtio-blk
Mysql: set the primary key to automatically increase the starting value
Mysql中事件和定时任务
【服务器安装mysql】centos7下使用mysql离线安装包安装mysql5.7
Research on a Consensus Mechanism-Based Anti-Runaway Scheme for Digital Trunking Terminals
[QNX Hypervisor 2.2用户手册]10.15 vdev timer8254
Network Skill Tree
jwsManager服务接口实现类-jni实现









