Sam
Sam

Reputation: 81

Make sync request with node js request module

I have an array of urls and want to request each of them with node js request module in a forEach loop becuase of large number of requests i want to execute each of theme one by one, but becauce of request module is async it execute all of theme, how can i force it to request one by one

Upvotes: 1

Views: 1891

Answers (1)

Julián Duque
Julián Duque

Reputation: 9727

You can also use async module to do a eachSeries iteration

Installation

npm install async --save

Code example

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
}

Upvotes: 1

Related Questions