0

I'm new to NodeJS and I'm currently working on node-soap module that can be found from https://github.com/vpulim/node-soap.

I'm calling a Web Service by using the referenced module. I need to return the SOAP client's response to user's web browser, however, the problem is that the response gets returned before it is fetched to an array. This is related to asynchronous way of working.

The second problem is that I need to call the Web Service again until I get specific amount of results. Each result will be pushed to the same array. How to do this? I need to return this array to user but as described before, it is always empty.

How would you use soap.createClientAsync and client.methodAsync in this case?

I have already tried writing a while-loop that continues until I get specific amount of results. I tried wrapping soap.createClient to a promise as well as soap.method. Those promises are in different functions and I tried to call them in async function which returns the array.

function createSoapClient() {
  return new Promise(function(resolve, reject) {
    var url = '...';

    soap.createClient(url, function(err, client) {
      if (err) {
        reject(err);
      }

      resolve(client);
    });
  });
}

function fetchServiceCustomers(client) {
  return new Promise(function(resolve, reject) {
    var args = {...};

    client.method(args, function(error, result, rawResponse, soapHeader, rawRequest) {
      if (error) {
        reject(error);
      }


      resolve(result);
    }, {timeout: 60 * 1000});
  });
}

exports.getServiceCustomers = async function() {
  let client = await createSoapClient();

  var results = 0,
      completeResult = [];
  while (results <= 0 || results >= 10000) {
        completeResult.push(await fetchServiceCustomers(client);
        results = completeResult[completeResult.length - 1];
        console.log(results);
  }
  return completeResult;
}
Mikael H.
  • 23
  • 8
  • I don't think I will be able to help you, but could you post your code? It will be difficult for others to help you otherwise. – Sentinel Jul 25 '19 at 06:34
  • @Sentinel, I have added some code. Is it useless to use promises, when there is async functions available? – Mikael H. Jul 25 '19 at 07:24
  • I am not sure if they are useless, but I would say that async is more readable then Promises. Have you tried the async functions? You would probably need to do something like `let client = await soap.createClientAsync(url); – Sentinel Jul 25 '19 at 07:46
  • `node-soap`'s async functions are working. I could try writing the while loop inside a promise and wait for it to resolve. – Mikael H. Jul 27 '19 at 14:33
  • well if they work, shouldn't you be able to just await client.method() and use the result of that function? There would be no need for while loops then? – Sentinel Jul 29 '19 at 06:45

0 Answers0