下面是我程序的伪代码
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