I have a function that connect to a web service in SOAP. Unfortunately the web service only support a very limited connections. I have an array of items to search in the web service, if i do a for or a foreach loop, the 70% of cases complete with no error, but in the 30% the web service response a error. This occurs when the max connections is overflow. This happens because the loop is no waiting the response of the webservice and the loop cotinues creating a lot of connections.
Here's my code:
var promiseArray = [];
for (var i = 0; i < result.length; i++) {
  let m = result[i].id
  let xml = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">' +
    '<soapenv:Header/>' +
    '<soapenv:Body>' +
    '<tem:EjecutarConsultaXML>' +
    '<!--Optional:-->' +
    '<tem:pvstrxmlParametros>' +
    '<![CDATA[' +
    '<Consulta><NombreConexion>USERNAME</NombreConexion>' +
    '<IdConsulta>QUERY</IdConsulta>' +
    '<Parametros>' +
    '<doc>' + m + '</doc>' +
    '</Parametros>' +
    '</Consulta>' +
    ']]>' +
    '</tem:pvstrxmlParametros>' +
    '</tem:EjecutarConsultaXML>' +
    '</soapenv:Body>' +
    '</soapenv:Envelope>';
  const options = {
    explicitArray: true
  };
  promiseArray.push(new Promise(async(resolve, reject) => {
    await axios.post(url, xml, {
        headers: {
          'Content-Type': 'text/xml;charset=UTF-8'
        }
      })
      .then((data) => {
        xml2js.parseString(data.data, options, (err, result) => {
          var temp = (result['soap:Envelope']['soap:Body'][0]['EjecutarConsultaXMLResponse'][0]['EjecutarConsultaXMLResult'][0]['diffgr:diffgram'][0]['NewDataSet'][0]['Resultado'])
          resolve({
            doc: m,
            state: temp[0].f430_ind_estado[0]
          })
        });
      })
      .catch((err) => {
        console.log(err)
      });
  }))
}
res.send(await Promise.all(promiseArray)) 
     
    