When call a throw from async function node says an error:
Unhandled promise rejection. This error originated either by throwing inside of an
asyncfunction without acatchblock, or by rejecting a promise which was not handled with.catch().
But i have a .catch() defined on call:
class Cls {
async fn(){
throw new Error('err');
return 'test';
}
}
cls = new Cls();
cls.fn()
.then(data => console.log(data))
.catch(e => { throw e })
What is the problem?
The catch here
cls.fn()
.then(data => console.log(data))
.catch(e => { throw e })
is actually catching an error thrown inside the fn method.:
throw new Error('err');
However, after you catch it, you explicitly raise another error
.catch(e => { throw e });
And now there's no one to capture this new error you throw, so your program crashes.