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
}
})
}
}
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;
}
}
},
这里是在post方法的回调里面,检测不到你的循环的,你break肯定报错,return也是return其中一个回调方法,其他方法还是继续执行的
你可以在post前面加个if(hasLockId) break;