title: Nodejs Return result from nested function
body: <p>I am having a hard time wrapping my head around how i can manipulate the stack in JS
  Hopefully this exercise will help me..</p>
  
  <p>So im trying write a function that makes a soap xml call and parses the data. Then returns the parsed data when called.</p>
  
  <p>I can get it to console.log the result when i call the function, but i can not get it to return the result so i can store it in a variable ect.. It returns undefined. </p>
  
  <p>Code Below:</p>
  
  <pre><code>var request = require('request');
  var DOMParser = require('xmldom').DOMParser;
  
  function query (entity,field,op,expression,elementToParse){
     xml = 
          `&lt;?xml version="1.0" encoding="utf-8"?&gt;
             &lt;soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
             xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
             &lt;soap:Header&gt;
             &lt;AutotaskIntegrations xmlns="http://autotask.net/ATWS/v1_6/"&gt;
             &lt;IntegrationCode&gt;H2FATPVKPDN25IXIBSMN5K66XAA&lt;/IntegrationCode&gt;
             &lt;/AutotaskIntegrations&gt;
             &lt;/soap:Header&gt;
             &lt;soap:Body&gt;
             &lt;query xmlns="http://autotask.net/ATWS/v1_6/"&gt;
             &lt;sXML&gt;&lt;![CDATA[&lt;queryxml&gt;&lt;entity&gt;${entity}&lt;/entity&gt;&lt;query&gt;&lt;field&gt;${field}&lt;expression 
             op="${op}"&gt;${expression}&lt;/expression&gt;&lt;/field&gt;&lt;/query&gt;&lt;/queryxml&gt;]]&gt;&lt;/sXML&gt;
             &lt;/query&gt;
             &lt;/soap:Body&gt;
             &lt;/soap:Envelope&gt;`;
  
  
     options = {
        'method': 'POST',
        'url': 'https://webservices15.autotask.net/ATServices/1.6/atws.asmx',
        'headers': {
           'Content-Type': 'text/xml',
           'Authorization':'Auth Goes Here'
        },
  
        body: xml
  
        };
  
      request(options, function (error, response) { 
        if (error) throw new Error(error);
         text =  response.body;
         parser =  new DOMParser();
         xmlDoc =  parser.parseFromString(text,"text/xml");
         xmlResult = xmlDoc.getElementsByTagName(`${elementToParse}`)[0].childNodes[0].nodeValue;
         **console.log(xmlResult)**
        });
  }
  
  query('Ticket','TicketNumber','BeginsWith','T2019','id') 
  // returns result if i log the data in REQUEST
  //returns undefined otherwise
  </code></pre>