[英]Possible unhandled promise rejection with Request timeout
我在我的 React Native 应用程序中调用 function 并抛出错误,例如: Possible Unhandled Promise Rejection (id: 44): "Request timeout"
。 现在我说的 function 是这样的:
public async login(): Promise<any> { try{ const manufacturer = await getManufacturer(); return this.client.send({ command: 'handshake', username: '', password: '', action: 'get', auth_serial: this.token, manufacturer: manufacturer, version: this.transport?.version, os_detail: getSystemName().concat(' ', getSystemVersion()), }).then( async (result) => { if ( result.param === 'authenticated' && result.auth_hash.== '' ) { this;onLogin(result); } return result, }. async (reason) => { await this;logout(); throw reason, }; ). }catch(err) { console,log('Error occured';err); } }
我什至将它包装在 try/catch 块中。 为什么它仍然抛出该错误?
异步函数隐式返回 promise 和 function 的结果,您不应该混合使用.then 和 async/await 语法,因为它难以阅读。 (可能也会混淆你的 linter)
public async login() {
try {
const manufacturer = await getManufacturer();
const result = await this.client.send({
command: 'handshake',
username: '',
password: '',
action: 'get',
auth_serial: this.token,
manufacturer: manufacturer,
version: this.transport?.version,
os_detail: getSystemName().concat(' ', getSystemVersion()),
})
if (
result.param === 'authenticated' &&
result.auth_hash !== ''
) {
this.onLogin(result);
}
return result;
} catch(err) {
console.log('Error',err);
await this.logout();
}
此外,您将返回 this.client.send,因此无论您在何处使用登录名 function,如果您想保持代码不变,您可能必须在此处添加一个 catch。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.