这是怎么回事? [ TsLint 错误: “承诺必须得到适当的处理”]

我正在使用 TypeScript 中的 async/await执行一些基本的异步操作,但是 TSLint 为下面的这两个函数抛出了神秘的错误消息。以前有人遇到过这些错误吗?在错误输出中没有提到治理规则,所以我不明白是什么导致了这些错误。有任何想法都会很感激。

主要要求:

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行的 Promises must be handled appropriatelyawait of non-Promise

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

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

这也得到 Promises must be handled appropriately

附加信息:

尽管错误消息没有提到它,但这似乎是 Promises must be handled appropriately的管理规则: Https://palantir.github.io/tslint/rules/no-floating-promises/

这期提到了 await of non-Promise: Https://github.com/palantir/tslint/issues/2661

90019 次浏览

Your getStatus function is defined to return a promise:

// All functions marked as async returns a promise:
async function getStatus(message: Message) {/* ... */}

But you called getStatus without calling it's then:

getStatus(message)

Therefore the compiler thinks you've forgotten to handle your async code. All you need to do is call .then():

getStatus(message).then(() => console.log('done'));

I think this problem is fixable by awaiting the getStatus function, since its an async function. The message is saying it clearly, but the line number causes confusion. To be honest it also took me some time.

You can solve this lint error by this change in code:

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

In my opinion it's not a good idea to switching these specific errors off. They are useful because in the other way you would not run the code async.

I have got the same exception when i have created firebase-function using firebase-tool

const ref = admin.database().ref("path/to/database/object");


ref.once("value").catch(error =>{  // line 22
response.send( error() );
}).then( snapshot =>{
response.send( snapshot.val );
})

This code doesn not compiled and return

ERROR: /src/index.ts[22, 5]: Promises must be handled appropriately

I have changed the places of catch and then.

ref.once(...).then(...).catch(...)

This code is work, i am sorry but I don't have any explanation

So much amazing when app return the some error without catch block even according firebase doc not mentioned that catch is required.

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

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

So, for example, if you do

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

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')

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'))

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.

Sometimes you might want to call the promise, but you don't need to do anything with the response. A route change or something else.

so instead of:

promiseFunction().then().catch()
try/catch async/await

you can do:

void promiseFunction();

As per the comments on the correct use of void please read: IgnoreVoid

Use at your own convenience :)

If you are writing a void async function with typescript-eslint, you will be getting a complaint if you write async functions without await, it's easy to forget especially for the last line of the function.

const sendSyncPlayCommandToWatchers = async (): Promise<void> => {
...
someAsyncFunction()
}

You will get

error Promises must be handled appropriately @typescript-eslint/no-floating-promises

You need to add the await keyword to the last line (or make sure you handle the promise with try/catch as mentioned in previous replies

const sendSyncPlayCommandToWatchers = async () => {
...
await someAsyncFunction()
}

I think this will also help getting a better stack trace when debugging.