Javascript 如何将请求响应传递给另一个请求?
在ExpressJS应用程序上使用RequestPromise模块,我想发出两个请求,但我需要从第一个请求生成的响应数据传递到第二个请求 我所追求的一个例子是Javascript 如何将请求响应传递给另一个请求?,javascript,node.js,http,express,request,Javascript,Node.js,Http,Express,Request,在ExpressJS应用程序上使用RequestPromise模块,我想发出两个请求,但我需要从第一个请求生成的响应数据传递到第二个请求 我所追求的一个例子是 const options = { url: 'http://api.example.com/v1/token', method: 'GET' }; request(options).then((response) => { request({ url: 'http://api.exampl
const options = {
url: 'http://api.example.com/v1/token',
method: 'GET'
};
request(options).then((response) => {
request({
url: 'http://api.example.com/v1/user',
method: 'POST',
data: { token: response.token }
}).then((final_response) => {
res.send(final_response);
});
});
为了使示例简短,我省略了错误处理。我感兴趣的是一种将响应从一个请求传递到另一个请求的技术。您可以通过返回承诺来链接承诺。 比如:
request(options1)
.then((response1) => {
return request(options2)
})
.then((response2) => {
return request(options3)
})
.then((final_response) => {
res.send(final_response);
});
这是一篇很好的文章。你知道如何将响应从第一个请求传递到第二个请求吗?你可以在父上下文中使用一个变量来“保存”结果。这里是一个感谢,我注意到
。然后((response1)=>{const this.response1=response1})
可以在第二个中访问。然后((response2)=>{const response1=this.response1})
。