【发布时间】:2020-04-07 11:15:11
【问题描述】:
我从承诺中无法真正理解一些东西。它曾经发生在我身上,也有回调。我不知道我没有看到什么。当我在“promise.then”中定义一个函数时,它可以正常工作。但是当我在外面用相同的参数定义相同的函数时,它说参数没有定义。这里发生了什么事?还有另一种方法可以让代码更简洁?
我发布了一个使用 express 和 axios npm 的代码,但我认为这不是问题。
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);
});
});
第二个代码
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);
});
}
【问题讨论】: