Friday, 28 April 2017

How to control request frequency when using Promise.all()

I need some data from another site, So i should request the site one by one, I put the in promises array and using Promise.all(promises) to do it fast.

But the problem is it is too fast, so the site server block me a while, How to control request frequency when using Promise.all()?

Following is my code:

(async() => {
  const request = require('request-promise')

  let promises = []
  let i = 10000
  while( i < 0){
    promises.push(new Promise((r) => {
      request({
        uri: 'http://somehost_i_need_the_data/' + i
      })
      r()
    }))
    i++
  }

  await Promise.all(promises)

})()



via Wooden

No comments:

Post a Comment