2014-10-28 2 views
1
var parsedUrl = req.body.parsedUrl; 
    console.log(parsedUrl); 
    var options = { 
     url: 'https://api.github.com/repos/' + parsedUrl, 
     headers: { 
      'User-Agent': 'request' 
     } 
    }; 

    var options2 = { 
     url: 'https://api.github.com/repos/' + parsedUrl + '/readme', 
     headers: { 
      'User-Agent': 'request' 
     } 
    }; 

function callback(error, response, body) { 
     if (!error && response.statusCode == 200) { 
      var info = JSON.parse(body); 
      Thing.create(info, function(err, thing) { 
      //console.log(info); 
      if(err) { return handleError(res, err); } 
      return res.json(201, thing); 
      }); 

     } else { 
     console.log('github request error'); 
     } 

    } 
    request(options, callback); 
    request(options2, callback); 

는 기본적으로,이 개 요청을해야합니다 1. 내가 어떻게 비동기 비동기 모듈을 사용하여 두 요청을 할 수 있습니다. async.parallel을 사용하려고 시도했지만 async.parallel이 콜백을 사용하기 때문에 구문이 요청의 구문과 잘 일치하지 않습니다.여러 요청을 비동기 적으로 같은 콜백 기능

2. 어떻게 병합 할 수 있습니까? Mongo의 같은 객체에 동시에 데이터베이스에 저장해야합니다.

나는 이것이 의미가 있기를 바랍니다. 감사합니다,

답변

1

당신은 async 라이브러리를 사용할 수 있습니다 : optionsoptions2와 배열을 만들기

npm install async --save 

을하고 배열의 각 항목에 request 전화 :

async.map([options, options2], request, function(err, results) { 
    // results[0] is the response for options 
    // results[1] is the response for options2 
    // you can merge them here and insert into mongodb as one document 
}); 
관련 문제