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.
The main request:
import * as rp from 'request-promise'
export function getRequest(address: rp.Options): rp.RequestPromise {
return rp(address)
}
Exported async function:
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)
}
}
This gets: Promises must be handled appropriately
and 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)
}
})
This also gets Promises must be handled appropriately
.
Additional information:
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/
And this Issue mentions await of non-Promise
:
https://github.com/palantir/tslint/issues/2661
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:
you can do:
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
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,or just disabling tslint for that line via:
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.I have got the same exception when i have created
firebase-function
usingfirebase-tool
This code doesn not compiled and
return
I have changed the places of
catch
andthen
.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 accordingfirebase doc
not mentioned thatcatch
is required.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:
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.
Your
getStatus
function is defined to return a promise:But you called
getStatus
without calling it's then:Therefore the compiler thinks you've forgotten to handle your async code. All you need to do is call
.then()
: