这可能是什么? [TsLint错误:“必须正确处理承诺”]] [英] What could this be about? [TsLint Error: "Promises must be handled appropriately"]

查看:612
本文介绍了这可能是什么? [TsLint错误:“必须正确处理承诺”]]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用TypeScript中的 async / await 做一些基本的异步操作,但是TSLint在下面的这两个函数中抛出了神秘的错误消息。有人遇到过这些错误吗?在错误输出中未提及管理规则,因此我不知道是什么原因造成的。

I'm doing some basic asynchronous operations using async/await in TypeScript but TSLint is throwing mysterious error messages for these two functions below. Has anyone encountered these errors before? On the error output the governing rule is not mentioned, so I don't understand what's causing these. Any ideas would be greatly appreciated.

主要要求:

import * as rp from 'request-promise'

export function getRequest(address: rp.Options): rp.RequestPromise {
  return rp(address)
}

导出的异步函数:

export async function getStatus(message: Message) {
  try {
    const res = await getRequest(address)
    if (res.ready) {
      message.reply('...')
    } else {
      message.reply('...')
    }
  } catch (err) {
    message.reply(err)
  }
}

这将得到:必须正确处理承诺,并在第3行中等待无承诺

This gets: Promises must be handled appropriatelyand await of non-Promise for line #3.

使用此导出功能的简单函数是:

The simple function that uses this export is:

client.on('message', message => {
  if (message.content === 'green') {
    getStatus(message)
  }
})

这也会得到承诺必须得到适当处理

其他信息:

即使错误消息未提及,但这似乎是必须正确处理承诺的管理规则
https://palantir.github.io/tslint/rules/no-floating-promises/

Even though the error message doesn't mention it, this seems to be the governing rule for Promises must be handled appropriately: https://palantir.github.io/tslint/rules/no-floating-promises/

问题提到等待无承诺
https://github.com/palantir/tslint/issues/2661

推荐答案

这是一个糟糕的错误信息。更好的方法可能是

That's a crappy error message. A better one might be,

每个类型 Promise 的表达式都必须以对<$的调用结尾c $ c> .catch 或调用 .then 并使用拒绝处理程序)。

every expression of type Promise must end with a call to .catch or a call to .then with a rejection handler (source).

例如,如果您这样做

PromiseFunction()
  .catch(err => handle(err))
  .then(() => console.log('this will succeed'))

然后您仍然会遇到tslint问题,因为 .then(...)的类型是一个保证,而且必须以catch结尾。该修补程序将附加一个 .catch 子句,例如,

then you will still have a tslint problem, because the type of .then(...) is a promise, and it has to end with a catch. The fix would be appending a .catch clause, for example,

PromiseFunction()
  .catch(err => handle(err))
  .then(() => console.log('this will succeed'))
  .catch(() => 'obligatory catch')

或仅通过以下方式为该行禁用tslint:

or just disabling tslint for that line via:

PromiseFunction()
  .catch(err => handle(err))
  // tslint:disable-next-line:no-unsafe-any
  .then(() => console.log('this will succeed'))

或者,您可以颠倒的顺序。然后 .catch 语句。但是,如果确实发生错误,这会阻止 .then 的执行,如果遇到此问题,您可能希望这样做。

Alternatively, you could reverse the order of the .then and .catch statements. However, that stops the .then from executing if an error does occur, which you presumably want if you encountered this problem.

这篇关于这可能是什么? [TsLint错误:“必须正确处理承诺”]]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆