title: How to Loop Through API Calls
body: <p>I am trying to loop through coordinates of an API endpoint and test each response. When I send the request when it is not nested in for loops it works just fine, however, it doesn't seem to send when nested.</p>
  
  <p>How can I automate the testing of this endpoint with various coordinates?</p>
  
  <pre><code>const request = require('request')
  const domain = 'host.local'
  const port = '8085'
  const url = 'http://' + domain + ':' + port + '/v1/vend/item'
  
  const parameters = {
   coordinate: {
     x: null,
     y: null
   },
   network: {
     user: "config",
     role: "admin"
   }
  }
  
  const x_coordinates = [1,2,3,4,5]
  const y_coordinates = [6,7,8,9,10]
  
  let options = {
    method: 'post',
    body: parameters,
    json: true,
    url: url
  }
  
  for (item in x_coordinates) {
    parameters.coordinate.x = parseInt(item) + 1
    for (item in y_coordinates.length) {
      parameters.coordinate.y = parseInt(item) + 7
      sleep(10000)
  
      request(options, (err, res, body) =&gt; {
        var headers = res.headers
        var statusCode = res.statusCode
      })
    }
  }
  
  function sleep(milliseconds) {
    var start = new Date().getTime();
    for (var i = 0; i &lt; 1e7; i++) {
      if ((new Date().getTime() - start) &gt; milliseconds){
        break
      }
    }
  }
  </code></pre>
  
  <p>Alternative promise method</p>
  
  <pre><code>for(let i=0; i&lt;x_coordinates.length; i++) {
    body.coordinate.x = i
    for(let j=0; j&lt;y_coordinates.length; j++) {
      body.coordinate.y = j
  
      let options = {
        url: 'http://' + domain + ':' + port + '/v1/vend/item',
        method: 'post',
        json: true,
        body: body
      }
  
      ps.push(rp(options))
  
    }
  }
  
  Promise.all(ps)
    .then((results) =&gt; {
        console.log(results)
    })
    .catch(err =&gt; {
      console.log(err)
    })
  </code></pre>
  
  <p>This implementation of promises sent all the requests at once. They need a delay between them. Ideally, once the first request gets a response, the second is sent.</p>