0

Have to take multiple SOAP requests and need to take back all the returns together.

What I do now is:

    for (let this_target of list_of_target)
    {
        const req = http.request(conn, (res) =>   {
            let data='';
            res.on('data', (chunk) =>
                   {
                       data += chunk;
                   });
            res.on('end', () =>
                   {
                       ... do some stuff with the result of the SOAP request ...

                       my_external_array.concat (myData);
                   });
        });

        var om_req={
            'CT_Get' : {
                ...some extra stuff...
                'target': this_target
                ...some extra stuff...
            }
        };

        var builder=new xml2js.Builder();

        var om_req_xml=builder.buildObject(om_req);

        req.write(om_req_xml);

        req.end();   
    }

ok

I know that I can't retrive the data outside this loop because the callback.

Curiously (or not), if I add a console.log(my_external_array) just after my_external_array.concat (myData);, the script shows the adding of the items after each processing... However if I put the same console.log outside the callback (anywhere) I don't have any return... :(

Which would be the best way to process all those request, join the results into an array and pass it to some another function. I need all the values from this requests to summarize them and save the summarized result into a database

Sorry being not more detailed about what I'm doing...

HufflepuffBR
  • 443
  • 3
  • 15

1 Answers1

2

This is a common problem with asynchronous operations. You could use Promises to solve your issue.

Here is a code snippet to give you an idea of how to use Promises:

const promisesList = [];
for (let this_target of list_of_target) {
  promisesList.push(new Promise((resolve, reject) => {

    // Your request code...

    // Make sure to resolve the promise with your transformed data, eg.:
    /*
      res.on('end', () => {
        // ... do some stuff with the result of the SOAP request ...

        resolve(myData);
      });
    */

  }));
}

// Process all the promises
Promise.all(promisesList).then(results => {
  // `results` is an array which contains all the resolved values.
  // This would be equivalant to `my_external_array` from your old code.

  console.log(results);
});
Teh
  • 2,767
  • 2
  • 15
  • 16
  • That worked... The worst problem is still to take the results outside the `Promise.all`. Anyway, this works for now – HufflepuffBR Sep 06 '19 at 17:24