【问题标题】:TsLint complains about calling top level async methodTsLint 抱怨调用顶级异步方法
【发布时间】:2019-01-26 13:53:35
【问题描述】:

我已将 TsLint 配置为警告未处理的承诺:

{
    "rules": {
        "no-floating-promises": [
          true,
          "Promise",
          "Q.Promise" 
        ],
    }
}

代码跟在standard template for build tasks for vsts:

async function run() {
  try {
      ...
  } catch (err) {
      tl.setResult(tl.TaskResult.Failed, err.message);
  }
}

run(); // tslint complains here...

TsLint 现在抱怨 promise 未处理,但我不能在 run 之前添加 await ...我不想将 run 与 .then 混合,因为它引入了另一个异步范例...

ERROR: C:/Users/JesseHouwing/Source/Repos/....ts[16, 5]: Promises must be handled appropriately

这些任务在 Node 中执行,而不是等待 run 方法,它工作正常......我可以安全地抑制它吗?有更好的方法吗?

【问题讨论】:

    标签: node.js typescript async-await tslint ecmascript-2016


    【解决方案1】:

    似乎我可以通过使用以下方法安全地抑制该值:

    async function run() {
      try {
          ...
      } catch (err) {
          tl.setResult(tl.TaskResult.Failed, err.message);
      }
    }
    
    void run(); // tslint no longer complains here and it's explicit :)
    

    在顶级run方法前面插入void似乎可以解决这个问题。

    【讨论】:

      猜你喜欢
      • 2021-04-21
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 2023-02-10
      • 1970-01-01
      • 1970-01-01
      • 2022-07-06
      • 2020-06-27
      相关资源
      最近更新 更多