I am calling a function in my React Native application and it's throwing error like: Possible Unhandled Promise Rejection (id: 44): "Request timeout"
. Now the function i'm talking about is like this:
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); } }
I have even wrapped it in try/catch block. Why is it still throwing that error?
Async functions implicitly return a promise with the result of your function, you shouldn't mix.then and async/await syntax as it's difficult to read. (Probably confusing your linter too)
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();
}
Also, you are returning this.client.send, so where ever you are using the login function you probably have to add a catch there if you want to keep your code how it is.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.