2

I am new to Node.js technology and facing some issues in recursive concept.

I have a main.js which contains list of username and a soap method call Soap.js contains soap method which will fetch email id from username.

------------- Main.js ----------------
'use strict'

var emailService = require('./emailService .js').emailService ;
var emailService1 = new emailService ();

var emailList = [];
var psList = ['1062','10465','10664','10681'];
emailService1.helpdeskEmailService(psList, 'abcabc', 'abcabc', function(err,result) {
    console.log('in service -------------------------');
    if (err) {
        console.log("Error while api call :: " +err);
    } else {
        console.log("response from soap service - " +  result);
  }

});

console.log('my email list' +result);

------------- SoapService.js ----------------

'use strict'

var c_instancename = '';
var soap = require('soap');
var l_args;
var c_url = "http://airinmsbmcarmt.lntinfotech.com/arsys/WSDL/public/172.21.103.136/zlandt:FetchEmailID";

class emailService {

    constructor(p_instanceName) {
        c_instancename = p_instanceName;
    }

    helpdeskEmailService (ps_number,p_username,p_password,p_callback) {
        var l_header = {                      
              'authentication': '',
              'locale': '',
              'timeZone': '',
              'AuthenticationInfo': {
              'userName': p_username,
              'password': p_password
              }
        }

        soap.createClient(c_url, function(err, client) {      
          //var soapheader = l_header;
            client.addSoapHeader(l_header);
            var l_args = {LoginID:ps_number};
            client.EmailID(l_args, function(err, result) {
              if(err) {
                  console.log('error page');
              } else {
                  console.log('my resultttttttt in soap...');
                  p_callback(err,result);
              }
            });
        });
    }
}

module.exports.emailService = emailService;

In this case, I'm getting late response from soap service.

Can I have sync call for webservice because I am getting NULL values for emailList.

I have a main.js which contains list of username and a soap method call.

Soap.js contains soap method which will fetch email id from username.

ArifMustafa
  • 4,617
  • 5
  • 40
  • 48

1 Answers1

0

You can return a promise from your service and if you're using Node 8.0+ you can also make the call synchronous. If not at least it makes it easier to deal with asynchronous call.

helpdeskEmailService (ps_number,p_username,p_password) {
    var l_header = {                      
          'authentication': '',
          'locale': '',
          'timeZone': '',
          'AuthenticationInfo': {
          'userName': p_username,
          'password': p_password
          }
    }

    return new Promise (function (resolve, reject) {
        soap.createClient(c_url, function(err, client) {      
          //var soapheader = l_header;
            client.addSoapHeader(l_header);
            var l_args = {LoginID:ps_number};
            client.EmailID(l_args, function(err, result) {
              if(err) {
                  console.log('error page');
                  reject(err);
              } else {
                  console.log('my resultttttttt in soap...');
                  resolve(result);
              }
            });
        });
    }
}

// You can then call like this
var promise = emailService1.helpdeskEmailService(psList, 'abcabc', 'abcabc');

promise.then((result) => {
    console.log("response from soap service - " +  result);
}).catch ( (err) => {
    console.log("Error while api call :: " +err);
});

You can also use await to make this synchronous (depending on the version of Node you're using):

async function waitForresult() {
    try
    {
        const result = await emailService1.helpdeskEmailService(psList, 'abcabc','abcabc');
        console.log(result);
    } catch (e) {
        console.log('Error occurred: ' + e);
    }
};

waitForresult();
Terry Lennox
  • 29,471
  • 5
  • 28
  • 40