使用节点 js 请求模块发出同步请求

Make sync request with node js request module

我有一个 url 数组,我想在 forEach 循环中用 node js 请求模块请求它们中的每一个,因为大量请求我想一个一个地执行每个主题,但是因为请求模块是异步它执行所有主题,我怎么能强制它一个一个地请求

您还可以使用 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
}