节点JS异步请求

时间:2015-08-06 04:18:38

标签: node.js mongodb promise

我有超过500,000个网址存储在集合(users2)中,如

{
    "_id" : ObjectId("55c0ae729a9109fc1af36680"),    
    "application_url" : "http://example.com/"
 },
 {
    "_id" : ObjectId("55c0ae729a9109fc1af36685"),    
    "application_url" : "http://example.com/"
 }
 .....

Node JS服务器必须获取每个文档的“application_url”,并在发出请求之前生成新的url。  它必须按顺序执行,并且必须等待每个响应才能转到下一个URL(当发出新请求时)。  最终结果(恰好是JSON)需要存储在集合中。

var Promise = require("bluebird");
var request = Promise.promisify(require("request"));

exports.getDetails = function(req,res){

console.log("getDetails");

var sequence = Promise.resolve();

req.db.Users2.find({},{application_url:1,_id:0}).exec().then(function(result) {                       

                        return result.reduce(function(sequence, applicationUrl) {

                            return sequence.then(function() {                             
                                //return applicationUrl;
                                //console.log(applicationUrl.application_url);

                                actualUrl = applicationUrl.application_url;

                                console.log("actualUrl::"+actualUrl);

                                var splitUrl = actualUrl.toString().split('/');
                                var splitid = (splitUrl[5]+"").split('?');
                                var getId = splitid[0];
                                var id = getId.substring(2);
                                var newUrl = splitUrl[0]+'//'+splitUrl[2]+'/'+'lookup?id='+id;

                                //console.log("newUrl::"+newUrl);

                                return newUrl;

                            }).then(function(newUrl) {

                                console.log("newUrl::"+newUrl);     

                                request(newUrl).then(function(response,body){
                                    console.log(JSON.stringify(response));

                                })



                            });
                        }, Promise.resolve());

                }).then(function() {    

                    console.log("Done");

                }).catch(function(err) {

                    console.log("Error");

                })

}

我能够一直工作直到新的url生成,并且不知道如何在收到响应之前等待请求,然后使用promises转到下一个url。

1 个答案:

答案 0 :(得分:3)

你错过了return。您必须从then回调中退回承诺,否则它将不会等待它,并立即与undefined解决。

return result.reduce(function(sequence, applicationUrl) {
    return sequence.then(function() {
        var actualUrl = applicationUrl.application_url;
        console.log("actualUrl::" + actualUrl);
        var splitUrl = actualUrl.toString().split('/');
        var splitid = (splitUrl[5] + "").split('?');
        var getId = splitid[0];
        var id = getId.substring(2);
        var newUrl = splitUrl[0] + '//' + splitUrl[2] + '/' + 'lookup?id=' + id;
//      return newUrl;           <-- these are pretty superfluous, btw
//  }).then(function(newUrl) {   <-´
        console.log("newUrl::" + newUrl);
        return request(newUrl).then(function(response, body) {
//      ^^^^^^
            console.log(JSON.stringify(response));
        })
    });
}, Promise.resolve());