与节点js请求模块进行同步请求
我有一个url的数组,并希望请求每一个节点js请求模块在一个forEach循环,因为我想要逐一执行每个主题的大量请求,但因为请求模块是asynchronous它执行所有的主题,我该如何强制它一一要求
您也可以使用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 }