关于for循环里的post请求回调时中断外部for循环的问题
JavaScript代码
onCombine(nodes) {
let hasLockId = false
for(let i = 0; i < nodes.length; i++){
post("/api/locks/getLockIdByNodeId", {nodeId: nodes[i].id}, res => {
if(res){
hasLockId = true
return
}
})
}
}
post请求里使用break会报错,使用return只跳出了post请求也无法停止onCombine函数执行,找不到解决的方法
请问这种情况该怎么解决
最后代码写成这样达到了想要的结果,虽然没采用这种连续发送请求的方法,最后把全部数据发到后端解决,但还是要感谢各位的答疑解惑
async onCombine(nodes){
const getApi = (node) => {
return new Promise((resolve) => {
post( "/api/locks/getLockIdByNodeId", {nodeId : node.id}, res => {
resolve(res)
})
})
}
let hasLockId = false
for (let i = 0; i < nodes.length; i++) {
const res = await getApi(nodes[i]);
console.log(res)
if (res) {
hasLockId = true;
break;
}
}
},