毁灭如果不是零或返回函数

问题描述 投票:0回答:1
为了消耗我所做的代码:

async function myFunction() {

   const result = await whatever();

   if (result == null) {
      return;
   }

   const [actual, data, here] = result;
   // process the destructured
}

现在称

whatever()

被称为很多,以下三合会重复了几次。有没有办法作为一个衬里?

我知道我可以做类似的事情,但是有很多时间我没有默认值,并且如果总体数组为null,则实际上想退出整个功能(因为它专门表示我们由于某种原因或其他原因无法获取数据)。
	

您不能从变量分配中。最好的选择只是进行快速虚假检查:


if...return...const


javascript typescript
1个回答
0
投票

从理论上讲,一个单线是可能的,但它使您处于比最初的更糟糕的位置。像带有iife电话的三元人:

const [actual, data, here] = result || ['default', {}, 'data'];

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.