当前位置:网站首页>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
边栏推荐
- MODIS 16 day data monthly / quarterly synthesis
- Codeforces round 690 (Div. 3) C. unique number conventional solution
- Druid integration shardingsphere appears xxmapper Reasons and solutions of XML error reporting
- [leetcode] day102 spiral matrix II
- Creation and inheritance of JS class
- Software recommendation - Installation
- 【南京农业大学】考研初试复试资料分享
- 1184. Distance between bus stops
- 期盼已久全平台支持-开源IM项目OpenIM之uniapp更新
- .NET 测试框架 xUnit,MSTest, Specflow 使用经验汇总
猜你喜欢

ArcGIS create vector

Caikeng Alibaba cloud Kex_ exchange_ identification: read: Connection reset by peer

How vscode mouse wheel enlarges the interface

Win10 download address

Creation and inheritance of JS class
[email protected]"/>ZBar source code analysis - img_ scanner. c | [email protected]

EventLoop event loop mechanism

How to prevent XSS in PHP

1184. Distance between bus stops

15. ARM embedded system: how to debug single board with PC
随机推荐
解决Eureka默认缓存配置导致时效性问题
Jupyter uses tips
Win10 download address
124 maximum path sum in binary tree
Envi5.3 open GF-1 WFV data
EC200U-CN模块的使用
Hping3 installation and use
regular expression
Software recommendation - Installation
Druid integration shardingsphere appears xxmapper Reasons and solutions of XML error reporting
Servlet框架(servlet+jsp)+Mysql实现的增删改查+分页(功能包学生信息录入、学生信息增删改查、分页等)
leetcode:162. 寻找峰值【二分寻找峰值】
简单使用 MySQL 索引
Envi grid resampling
The 3D sensing market is accelerating. Who will be better, TOF or structured light?
Analysis of double pointer sliding window method and solution of leetcode related problems
js,for循环内callback异步转换成同步执行
[LeetCode]巧用位运算
TCP protocol debugging tool tcpengine v1.3.0 tutorial
Solve the timeliness problem caused by Eureka's default cache configuration