title: How to wait till the response comes from the $resource request, in angularjs?
body: <p>I am going to tell you a bit about what I am doing so you may help me. I am reading some records from a .txt and for <strong>each</strong> record I need to make some verifications, in order to do this, I make a $resource call to my backend service to get the <strong>regex</strong> which will be use to verified <strong>each record.</strong></p>
  
  <p><strong>I need to wait for the response of the service</strong>. I have this (I use $recourse for the get with .factory) :</p>
  
  <pre><code>function findRegex() {
          var data;
          var deferred = $q.defer();
          data = findRegexCountry.get({ number: $scope.number }).$promise.then(function(data) {
              deferred.resolve(data.body);
          });
          return deferred.promise;
      }
  </code></pre>
  
  <p>and I do can, successfully, use it in the function I need like this: </p>
  
  <pre><code>//validar tipo 2
          var bool = false;
          if (tipo == 2) {
              if (numbers.test(split[0]) &amp;&amp; numbers.test(split[1])) {
                  $scope.number = '58';
  
                  findRegex().then(function(data) {
                      bool = true;
                      // console.log('data', data);
                      $scope.regexResponse = data;
                      regex_all = data.regex_all;
                      regex_length = data.regex_length;
                      regex_justCountry = data.regex_justCountry;
                      regex_justCarriers = data.regex_justCarriers;
  
                      console.log('1. regex_justCarriers ', regex_justCarriers);
                      console.log('1. regex_length ', regex_length);
                  });
  
                  //nothing here :( SOS
                  console.log('2. regex_justCarriers ', regex_justCarriers);
  </code></pre>
  
  <p>This works, but I <strong>NEED</strong> to wait for the response in order to continue with my code, let me tell you why (maybe you can give me a better idea).</p>
  
  <p>I can't make all the logic inside <strong>findRegex()</strong>, first of all because I don't want to make this call <em>everytime</em> I read a record from the .txt. I want to get the regex from the service one time and if this first record I am checking is a valid record for me, the other ones will be using the variables I already got with the first call to the service, there are other reasons, but mainly this, I can't make all the code I need to inside this. </p>
  
  <p>What can I do?</p>