节点JS请求多个URL同时

问题描述:

我想请求与新公共管理的要求库中的多个网址,并在JSON返回结果节点JS请求多个URL同时

我的代码是,但不工作。

request({ 
    url: "https://www.googleapis.com/youtube/v3/commentThreads?part=snippet&videoId="+item+"&key=AIzaSyCPv-dasd&maxResults=100&", 
    url: "https://www.googleapis.com/youtube/v3/commentThreads?part=snippet&videoId="+item+"&key=AIzaSyCPv-dasd&maxResults=100&", 
    json: true 
}, 
+0

又该它返回? – niceman

+0

并不重要,它不工作 – user8026867

+0

它应该返回什么? :3(它确实很重要:)) – niceman

您可以使用类似promisify的蓝鸟,为了使用承诺:

const bluebird = require('bluebird'); 
const promiseRequest = bluebird.promisify(require('request')); 

Promise.all([ 
    promiseRequest(//request1 here), 
    promiseRequest(//request2 here), 
]) 
.then(function(res1, res2) { 
    //Deal with results here 
}) 
.catch(function(error) { 
    //Deal with error 
}); 

如果你提到的请求库this,那么它允许每个请求只有一个网址,所以你需要为每个需要URL单独调用。

希望它有帮助。

+0

谢谢我用asynx找到它 – user8026867