Manu Ruiz Ruiz
Manu Ruiz Ruiz

Reputation: 393

promise.then functions only works if defined inside

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);
        });
}

Upvotes: 0

Views: 59

Answers (1)

Dilshan
Dilshan

Reputation: 3001

You don't have to pass response,

app.get('/', function(req, res) {
    //enviamos un mensaje a auth
    axios
        .post('http://localhost:8081', {
            mensaje : 'Empiezo en api-rest.'
        })
        .then(respuestaDeAuth) // <-- Don't call your callback. Just pass it
        .catch(function(error) {
            console.log(error);
        });
});

when you add then(respuestDeAuth(response)), respuestDeAuth function immediately executed with a undefined variable call response. That is why its say response is not defined.

You can do a little experiment to understand this by declaring a variable in outer lexical environment like, const response = "Some data". Then comment the axios request and try to console.log() the response. This time you will not see a error instead you will see the value of response variable

EDIT

And if you want to add the the res parameter,

 app.get('/', function(req, res) {
        //enviamos un mensaje a auth
        axios
            .post('http://localhost:8081', {
                mensaje : 'Empiezo en api-rest.'
            })
            .then(() => respuestaDeAuth(res))
            .catch(function(error) {
                console.log(error);
            });
    });

The only thing you need to care is, NOT to call the callback function. Here I used a function and inside that function I called the respuestaDeAuth() with the original res object.

Upvotes: 2

Related Questions