当前位置:网站首页>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
边栏推荐
- Druid integration shardingsphere appears xxmapper Reasons and solutions of XML error reporting
- Simply use MySQL index
- Jupyter uses tips
- EMQ Yingyun technology was listed on the 2022 "cutting edge 100" list of Chinese entrepreneurs
- JUC source code learning note 3 - AQS waiting queue and cyclicbarrier, BlockingQueue
- [leetcode] skillfully use bit operation
- Creation and inheritance of JS class
- Causes and solutions of QT signal and slot connection failure
- [Nanjing Agricultural University] information sharing of postgraduate entrance examination and re examination
- MODIS 16 day data monthly / quarterly synthesis
猜你喜欢

我们为什么要推出Getaverse?

TCP protocol debugging tool tcpengine v1.3.0 tutorial

ArcGIS pixel size changed from 0.00025 to meters

15、ARM嵌入式系统:如何用PC调试单板

1184. Distance between bus stops
[email protected]"/>ZBar project introduction and installation configuration| [email protected]

Complete guide on how to prevent cross site scripting (XSS) attacks

About SQL data query statements

【南京农业大学】考研初试复试资料分享

Summary of experience in using.Net test framework xUnit, mstest, specflow
随机推荐
简易版QQ?Qt也可以实现!(一)
[Nanjing Agricultural University] information sharing of postgraduate entrance examination and re examination
ZCMU--5023: 家庭划分(C语言)
Due to lack of funds, Changdian technology sold some assets of Xingke Jinpeng for 120million US dollars!
详解 Apache Hudi Schema Evolution(模式演进)
GEO satellite data download
ArcGIS create vector
1184. 公交站间的距离
Qt信号和槽连接失败原因及解决办法
Software recommendation - office software
Qt设计仿真机器人控制器
Four common post submission methods (application / x-www-form-urlencoded, multipart / form data, application / JSON, text / XML)
Qt键盘事件(一)——检测按键输入
Servlet framework (servlet+jsp) + addition, deletion, modification and query + paging implemented by MySQL (function package student information entry, addition, deletion, modification and query of st
[leetcode] skillfully use bit operation
我们为什么要推出Getaverse?
[leetcode]75. color classification - problem solving (execution time beat 90%, memory consumption beat 78%)
Replace the image source of Debian full version with Alibaba cloud image source
Caikeng Alibaba cloud Kex_ exchange_ identification: read: Connection reset by peer
Chapter 2 using API Mgmnt service