node.js - 处理嵌套请求中的节点请求响应
问题描述
下面是我的程序的伪代码
1. Call a webservice which returns a json response
2. Parse it to get an id
3. Use the above id to call a different url to get more information
4. the above request return a list of items in json format
5. I am using the above items to call another webservice to get information specific to these items
6. I am making a custom json to concatenate the json for all these items
7. I can see the required format in my console.
问题甚至在 json 被构造/处理之前,响应就被发送到客户端。根据在谷歌上的搜索,我正在使用“request-promise-native”模块。
下面是我的代码片段。
var json ={}
var jsonkey='templates';
var optionsrp1 = {
url:''
header:''
}
rp1(options)
.then(function (body) {
// this return id
rp2(options1)
.then(function (body) {
// use the id to get items
rp3(options2)
.then(function (body) {
body.value.foreach() {
rp4(optionx).then(){
json.key.....
.........
}
res.send(jsonresponse)------------> this statement sends response without even waiting for the loop to finish. I am sure the change has to be in the above loop to trigger a promise for each request but not sure how to attain it. The number of items is dynamic.
})
.catch(function (err) {
// API call failed...
});
})
.catch(function (err) {
// API call failed...
});
})
.catch(function (err) {
// API call failed...
});
any help or pointers will be appreciated
解决方案
推荐阅读
- python - FileNotFoundError: [Errno 2] 没有这样的文件或目录 (Heroku)
- azure-devops-rest-api - 访问控制条目 - 我在哪里可以获得“令牌”
- bash - 从 git 下载文件的所有版本
- html - 我无法更改从 Google 字体复制的字体样式的字体大小
- javascript - 为什么当我调用我的 api 时我的 json 对象中的空值键被删除
- opencv - 质心的替代跟踪算法?
- activex - 什么是 Activex 控件?
- c# - c# 不更新数据库
- swift - 为什么当我导航到 SideMenuController 时 UITableView 自动高度不起作用
- javascript - React - 即使刷新了如何保持在同一页面上?