Node.js 等待,然后,不要得到Axios的承诺
我正在通过我正在构建的应用程序进行跟踪。我已经列出了一些步骤,以确定哪些被调用,哪些没有被调用 我调用Axios并尝试调用.then()从返回的承诺中获取数据,但它从未执行过。该应用程序一直在运行:Node.js 等待,然后,不要得到Axios的承诺,node.js,async-await,axios,Node.js,Async Await,Axios,我正在通过我正在构建的应用程序进行跟踪。我已经列出了一些步骤,以确定哪些被调用,哪些没有被调用 我调用Axios并尝试调用.then()从返回的承诺中获取数据,但它从未执行过。该应用程序一直在运行: module.exports = async function request(requestOpts = {}) { console.log("5: in request... sending request") const response = await sendR
module.exports = async function request(requestOpts = {}) {
console.log("5: in request... sending request")
const response = await sendRequest({
method: requestOpts.method,
url: requestOpts.url,
headers: requestOpts.headers,
}).then(function(response) {
console.log("7: in request.. returning response")
return response;
})
};
function sendRequest(requestOpts = {}) {
console.log("6: in sendRequest.. returning axios")
return (
axios({
method: requestOpts.method,
url: requestOpts.url,
headers: requestOpts.headers,
}).then((response) => {
console.log("Then ===> " + response)
return response;
}).catch((error) => {
console.log("Error ==> " + error)
return error;
})
);
}
第七步永远不会出现。。一切都在继续并完成。我的设置正确吗?以下是调用模块:
module.exports = async function axiosCaller() {
console.log('4: in axiosCaller')
const authHeader = buildBasicAuth();
let response = await request({
url: 'redacted',
method: 'get',
headers: {
authHeader
},
}).then((response) => {
console.log(response);
return handleResponse(response);
});
}
我认为您不应该在中返回异步响应,而是需要像承诺一样返回axios响应 异步sendRequest(){ 返回等待axios(…) }
在调用sendRequest函数时,您需要在前面提到wait或简单地使用。然后捕获响应yesss。就这样。我花了一整天在这上面。。。。。非常感谢。