如何使一个条件语句继续休息如果代码



如何获取此条件以继续,转到代码的下一部分。我设法通过放一个console.log来做到这一点,但我仍然认为第三点是一个更干净的解决方案。

// condition
if (conditionA === conditionB || conditionA === '') {
if (link.closest('.somothing')) { /*go to the rest of the code*/ } else { return }
}    
// Flow Rest of the code ......

同步执行

用你的";代码的下一部分";并称之为

链接:Javascript函数

// condition
if (conditionA === conditionB || conditionA === '') {
if (link.closest('.somothing')) { 
/*go to the rest of the code*/
nextCode();
} else {
return 
}
}    
// Flow Rest of the code ......
function nextCode() {
console.log("call this");
}

异步执行

如果你有异步源代码,另一种方法可以帮助你保持代码的整洁。

链接:Javascript Promise

new Promise((resolve) => {
console.log("execute 1");
// condition
if (conditionA === conditionB || conditionA === '') {
if (link.closest('.somothing')) {
/*go to the rest of the code*/
console.log("execute 2");
} else { 
return 
}
}    
}).then(() => {
console.log("execute 3");    
});
// Flow Rest of the code ......

您可以反转条件并提前返回。

if (conditionA === conditionB || conditionA === '') {
if (!link.closest('.somothing')) return;
}    

相关内容

最新更新