I am trying to return the value in a promise but I receive the following data
Promise {< pending >}
my code is the following
const respuesta = axios.get(`http://localhost:3000/parts/part/${this.codigo}`)
respuesta.then((response) => {return response.data.exists}) //<--Aqui reciboPromise {< pending >}
//En la promesa me retorna un booleano ya sea true o false el cual pretendo utilizar en un if y asignar logica
if (respuesta) {
//logica }
else {
//logica
}
I don't have much experience in promises and I'm trying to understand how they work if you could support me thanks regards
Your problem is that requests to APIs are asynchronous, that is, when you make a request, the code can continue advancing without having obtained its response (it can be given during the request, at the end of the request, or never).
In your case, this is what is happening, it is returning a promise.
Your problem has an easy solution:
As you can see, the code is exactly the same, but with two keywords inside.
With these two solutions, your code should already be functional.
But, in the event that it did not work, I recommend you make the following change:
That is, move the if/else inside the .then of the request to the api.