当前位置:网站首页>JS, call in the for loop is asynchronously converted to synchronous execution
JS, call in the for loop is asynchronously converted to synchronous execution
2022-07-24 16:31:00 【Fat as watermelon】
If for There are asynchronous methods in the loop , Will not wait for the asynchronous execution to complete , Just execute the following code , as follows :
//callback => Promise
function get(element){
// Impersonate asynchronous request
setTimeout(()=>{
if(element==5){
console.log(' error ')
return ' error '
}else{
console.log(' correct ')
return ' correct '
}
},100)
}
function down(){
let array=[1,2,3,4,5,6]
for (let index = 0; index < 5; index++) {
const element = array[index];
get(element)
}
console.log(' Cycle is completed ')
}
down()
// Cycle is completed
// correct
// correct
// correct
// correct
// error
Suppose you want to wait for the asynchronous execution to complete before executing the following code , That is, asynchronous to synchronous , If it is to use callback Asynchronous implementation , Need to put callback convert to Promise, The code is as follows :
function get(element){
return new Promise((resolve,reject)=>{
// Impersonate asynchronous request
setTimeout(()=>{
if(element==5){
reject(' error ')
}else{
resolve(' correct ')
}
},500)
})
}
async function down(){
let array=[1,2,3,4,5,6]
for (let index = 0; index < 5; index++) {
const element = array[index];
try {
let res = await get(element)
console.log(res)
} catch (error) {
console.log(error)
return
}
}
console.log(' Cycle is completed ')
}
down()
// correct
// correct
// correct
// correct
// error
边栏推荐
- JUC源码学习笔记3——AQS等待队列和CyclicBarrier,BlockingQueue
- VSCode如何鼠标滚轮放大界面
- ZBar source code analysis - img_ scanner. c | [email protected]
- Qt键盘事件(一)——检测按键输入
- leetcode:162. 寻找峰值【二分寻找峰值】
- With notes: printing order of synchronous, asynchronous, micro task and macro task
- You really should go to the factory to move bricks!
- MySQL converts strings to numeric types and sorts them
- Jia Yueting's Faraday will receive another financing of US $225million in the future, and ff91 will be mass produced soon!
- Matlab writes excel string and number merging
猜你喜欢

Using native JS to realize magnifying glass function

15. ARM embedded system: how to debug single board with PC

Envi5.3 open GF-1 WFV data

1184. Distance between bus stops

如何防止跨站点脚本 (XSS) 攻击完整指南

Telephone system rules

C# TCP客户端窗体应用程序异步接收方式

Mobile phone comparison redmi note8 and realm x2

Getting started with ARP

Mcd12q1 data shows multiple classifications in envi
随机推荐
【机器学习基础】——另一个视角解释SVM
Talk about C pointer
EC200U-CN模块的使用
简单工厂模式都不会,你真应该去工厂搬砖!
Qt设计机器人仿真控制器——按键控制机器人关节转动
ARP 入门
[leetcode] day102 spiral matrix II
简易版QQ?Qt也可以实现!(一)
[leetcode]38. counting - problem solving (execution time beat 91%, memory consumption beat 97%)
To create a private Ca, I use OpenSSL
Qualcomm reconciled with apple and received at least $4.5 billion in settlement fees! Next goal: Huawei!
Causes and solutions of QT signal and slot connection failure
"Heaven and the world, self-respect" -- single case mode
ArcGIS pixel size changed from 0.00025 to meters
Quickly view the version of redis in the server
Analysis of double pointer sliding window method and solution of leetcode related problems
[LeetCode]38.报数——题解(执行用时击败91% ,内存消耗击败 97%)
Qt设计仿真机器人控制器
.NET 测试框架 xUnit,MSTest, Specflow 使用经验汇总
C TCP client form application asynchronous receiving mode