cin*_*l45 39 javascript asynchronous exception typescript tslint
我正在使用async/awaitTypeScript 进行一些基本的异步操作,但TSLint正在为下面这两个函数抛出神秘的错误消息.有没有人遇到过这些错误?在错误输出上没有提到管理规则,所以我不明白是什么导致这些.任何想法将不胜感激.
主要要求:
import * as rp from 'request-promise'
export function getRequest(address: rp.Options): rp.RequestPromise {
return rp(address)
}
Run Code Online (Sandbox Code Playgroud)
导出的异步功能:
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)
}
}
Run Code Online (Sandbox Code Playgroud)
这得到:Promises must be handled appropriately并且await of non-Promise对于第3行.
使用此导出的简单函数是:
client.on('message', message => {
if (message.content === 'green') {
getStatus(message)
}
})
Run Code Online (Sandbox Code Playgroud)
这也得到了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
War*_*zee 72
这是一个糟糕的错误信息.一个更好的可能是,
每个类型的表达式Promise必须以对拒绝处理程序.catch.then(源)的调用或调用结束.
所以,例如,如果你这样做
PromiseFunction()
.catch(err => handle(err))
.then(() => console.log('this will succeed'))
Run Code Online (Sandbox Code Playgroud)
然后你仍然会有一个tslint问题,因为类型.then(...)是一个承诺,它必须以一个catch结束.该修复程序将附加一个.catch子句,例如,
PromiseFunction()
.catch(err => handle(err))
.then(() => console.log('this will succeed'))
.catch(() => 'obligatory catch')
Run Code Online (Sandbox Code Playgroud)
或者只是通过以下方式禁用该行的tslint:
PromiseFunction()
.catch(err => handle(err))
// tslint:disable-next-line:no-unsafe-any
.then(() => console.log('this will succeed'))
Run Code Online (Sandbox Code Playgroud)
或者,您可以颠倒.then和.catch语句的顺序.但是,.then如果发生错误,则会停止执行,如果遇到此问题,您可能会想要这样做.
T04*_*435 22
有时您可能想兑现承诺,但是您无需对响应做任何事情。路线变更或其他。
所以代替:
promiseFunction().then().catch()
try/catch async/await
Run Code Online (Sandbox Code Playgroud)
你可以做:
void promiseFunction();
Run Code Online (Sandbox Code Playgroud)
我已经得到了同样的异常,当我创建firebase-function使用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 );
})
Run Code Online (Sandbox Code Playgroud)
这段代码没有编译和 return
ERROR: /src/index.ts[22, 5]: Promises must be handled appropriately
Run Code Online (Sandbox Code Playgroud)
我已经改变了catch和 的位置then。
ref.once(...).then(...).catch(...)
Run Code Online (Sandbox Code Playgroud)
此代码有效,很抱歉,但我没有任何解释
当应用程序返回一些没有catch阻止的错误时,即使firebase doc没有提到这catch是必需的,这真是太神奇了。
您的getStatus函数被定义为返回一个承诺:
// All functions marked as async returns a promise:
async function getStatus(message: Message) {/* ... */}
Run Code Online (Sandbox Code Playgroud)
但是你getStatus没有打电话就打电话给它:
getStatus(message)
Run Code Online (Sandbox Code Playgroud)
因此编译器认为您忘记处理异步代码。您需要做的就是调用.then():
getStatus(message).then(() => console.log('done'));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21854 次 |
| 最近记录: |