The exception of throwing when catching to the upper level with an asynchronous function

Upper level throw error in asynchronous function

it

async create(body: NewDevice, firstTry = true): Promise<RepresentationalDevice> { try { return await this.dataAccess.getAccessToken() } catch (error) { throw error } } 

VS it

 async create(body: NewDevice, firstTry = true): Promise<RepresentationalDevice> { return await this.dataAccess.getAccessToken() } 

I mean, at the end, at the top level, I should still catch the error and there will be no change in catch at all

Are these two approaches the same? Can I use the second approach without error handling problems?

+5
source share
1 answer

This has nothing to do with asynchronous functions. Catching a mistake just to repair it is the same thing as not catching it in the first place. I.e.

 try { foo(); } catch(e) { throw e; } 

and

 foo(); 

basically equivalent, except that the stack trace may be different (since in the first case the error occurs elsewhere).

0
source

Source: https://habr.com/ru/post/1271207/


All Articles