async function myFunction() {
const result = await whatever();
if (result == null) {
return;
}
const [actual, data, here] = result;
// process the destructured
}
现在称whatever()
我知道我可以做类似的事情,但是有很多时间我没有默认值,并且如果总体数组为null,则实际上想退出整个功能(因为它专门表示我们由于某种原因或其他原因无法获取数据)。
您不能从变量分配中。最好的选择只是进行快速虚假检查:
if...return...const
从理论上讲,一个单线是可能的,但它使您处于比最初的更糟糕的位置。像带有iife电话的三元人:
const [actual, data, here] = result || ['default', {}, 'data'];