与节点js请求模块进行同步请求

时间:2015-07-30 18:39:23

标签: javascript node.js request

我有一个url数组,想要在forEach循环中请求每个节点js请求模块,因为我想逐个执行每个主题的大量请求,但是请求模块的问题是async它执行所有主题,我如何强制它逐一请求

1 个答案:

答案 0 :(得分:1)

您还可以使用async模块进行eachSeries次迭代

<强>安装

npm install async --save

代码示例

var async = require('async')
var urls = [ 'http://example1.com', 'http://example2.com' ]

async.eachSeries(urls, function (url, next) {
  request(url, function (e, r, b) {
    if (e) next(e) // handle error

    // i'm done, go to the next one!
    next() 
  })
}, onFinished)

function onFinished (err) {
  // I finished all the urls
}
相关问题