There is something that I dont really understand from promises. It used to happen to me with callbacks too. I don't know what I'm not seeing. When I define a function inside "promise.then", it works correctly. But when I define the same function with the same parameter outside, it says that the parameter isn't defined. What's happening here? There is another way to have a cleaner code?
Im posting a code that uses express and axios npm, but I don't think that's a problem.
app.get('/', function(req, res) {
//enviamos un mensaje a auth
axios
.post('http://localhost:8081', {
mensaje : 'Empiezo en api-rest.'
})
.then(function(response) {
//Ahora tengo que enviar la respuesta a priv
axios
.post('http://localhost:8082', response.data)
.then(function(responsePriv) {
console.log(responsePriv);
})
.catch(function(error) {
console.log(error);
});
})
.catch(function(error) {
console.log(error);
});
});
Second code
app.get('/', function(req, res) {
//enviamos un mensaje a auth
axios
.post('http://localhost:8081', {
mensaje : 'Empiezo en api-rest.'
})
.then(respuestaDeAuth(response))
.catch(function(error) {
console.log(error);
});
});
function respuestaDeAuth(response) {
//Ahora tengo que enviar la respuesta a priv
axios
.post('http://localhost:8082', response.data)
.then(function(responsePriv) {
console.log(responsePriv);
})
.catch(function(error) {
console.log(error);
});
}