使用请求与后处理

时间:2016-10-14 10:01:05

标签: node.js asynchronous request

我想多次执行请求并对其进行处理。 因为我在班上有一个结果数组,所以每次我调用它时都要存储请求的结果。实际上,请求可能有多个结果(即result.links.next)

但是,请求只执行了一次,并没有进入异步系列的下一个功能

我做错了什么?

APIManager.prototype.postTreatment = function (error, response, body) {

 if(error) return console.log("error :: ",error)

 if(response.statusCode !== 200) return console.log("Invalid statusCode returned :: ",response.statusCode);
 var result = JSON.parse(body);

 _.each(result.list,function(elem){
   this.results.push(elem);
 }.bind(this));

 //next result links
 this.nextUrl = result.links.next;
};

APIManager.prototype.request = function() {

  var authenticationHeader = "Basic " + new Buffer(this.config.login + ":" + this.config.password).toString("base64");

 url = this.config.host + this.config.api_people;

 var self = this;
 async.series([
    function(callback){
    request({
    url : url,
    headers : {
      "Authorization" : authenticationHeader,
      "Content-Type" : "application/json"
    }
  },self.postTreatment.bind(self));
},
function(){
  console.log("request :: nextUrl",this.nextUrl);
  console.log("request :: results",this.results);
  while(this.nextUrl !== false){
    console.log("there");
    request({
      url : this.nextUrl,
      headers : {
        "Authorization" : authenticationHeader,
        "Content-Type" : "application/json"
      }
    },self.postTreatment).bind(self);
  }
 }
 ],function(err, results) {
   console.log(err,results);
 });

};

0 个答案:

没有答案