promise.then functions only works if defined inside

你说的曾经没有我的故事 提交于 2021-01-29 12:10:39

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/61078745/promise-then-functions-only-works-if-defined-inside

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!