首页 > 解决方案 > 处理嵌套请求中的节点请求响应

问题描述

下面是我的程序的伪代码

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

标签: node.jsrequest-promise

解决方案


推荐阅读