我有一个 url 数组,并且想在 forEach 循环中使用 Node js 请求模块请求每个 URL,因为有大量请求,我想逐个执行每个主题,但因为请求模块是异步的执行所有主题,我怎样才能强制它一一请求
最佳答案
您还可以使用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
}
关于javascript - 使用 Node js 请求模块发出同步请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31731599/