I have the following method:
getDatos(){
let msj;
(this.escuderia == undefined || this.piloto == undefined || this.posicion == undefined)
? (msj = `Faltan Datos!` //aqui deberia ir el throw)
: (msj = `Escuderia: ${this.escuderia}, Piloto: ${this.piloto}, Posicion: ${this.posicion}`)
return msj
}
what I want in case the condition is met, I get an error message using throw but I can't do it, it throws me syntax errors and I don't know why. What I do is:
? (throw "mensajedeerror")
I try to do that instead of saving the msg in a variable and VSCode gives me syntax errors.
The ternary operator expects two expressions to return something,
throw
it does not meet that condition. In any case, this logic is complex to read, the code could be rewritten like this:If you want to use it
throw
in ternary form, you can wrap it in a function and execute it if the condition is true: