title: How to assign returned data to variable - angularjs
body: <p>The SendOTP function works fine and returns data. I am trying to assign that to a variable.</p>
  
  <p><strong>JS:</strong></p>
  
  <pre><code>var SentStatus = "";
  DataFactory.SendOTP('39487539847')
  .success(function (data, status) {
  console.log('1 : ' + data);
  console.log('2 : ' + JSON.parse(data));
  SentStatus = JSON.parse(data);
  })
  .error(function (data, status) {
  SentStatus = JSON.parse(data);
  });
  console.log('3 : ' + SentStatus);
  </code></pre>
  
  <p>In the browser console I see:</p>
  
  <pre><code>3 :
  1 : "\"200\""
  2 : 200 
  </code></pre>
  
  <ol>
  <li>Why is the SentStatus empty. </li>
  <li>How come the order is 3,1,2. Shouldnt
  this be 1,2,3</li>
  </ol>
  
  <p><strong>EDIT:</strong></p>
  
  <pre><code>var SentStatus = "";
  DataFactory.SendOTP('39487539847')
  .then(function (data, status) {        
  SMSSentStatus = JSON.parse(data);
  })
  .catch(function (data, status) {
  SMSSentStatus = JSON.parse(data);
  })
  .finally(function () {
  console.log('3 : ' + SMSSentStatus);
  });
  
  dataFactory.SendOTP = function (mobileNumber) {
  return $http({
    url: GlobalVariables.vcAPIBaseUrl + "SendOTP",
    method: "GET",
    params: {
      mobileNumber: mobileNumber                
    }
    }).success(function (data, status) {            
    }).error(function (data, status) {            
    });        
  };
  </code></pre>
  
  <p><strong>Solved:</strong>
  The .then(),catch() and .finally() worked. I was just not reading the data correctly.
  It had to be <code>SMSSentStatus = JSON.parse(data.data)</code> in my case.</p>