1
I would like to know what is the "correct way" to write a Try/catch block:
async function ping() {
try {
undefined();
return 'pong';
} catch (error) {
console.log('fn ping:', error);
}
}
async function chamar() {
try {
const resultado = await ping();
console.log(resultado);
} catch (error) {
console.log('fn chamar:', error);
}
}
chamar();
Output when calling a function not defined within the "ping" function with Try/catch:
fn ping: TypeError: undefined is not a function
However, when Try/catch is not used in the ping function, the result is as follows::
fn chamar: TypeError: undefined is not a function
Which would be the correct way since the ping function will always be called inside a Try/catch. Also, I want to treat all errors in the "call" function. Have a problem not using Try/catch in functions that will already be called within another Try/catch?
Where is this function
pong
? Also, is there a reason you are annotating these functions withasync
?– Luiz Felipe
The correct was function
ping
, sorry for the error. I sent only one example, but in the project I will use the asynchronous functions– Bruno Sá