title: Parse data from server with $resourse
body: <p>How can I return the root from this API call in this factory? Now its undefined. I understand that this is because the ajax call is async but how to wait for it?</p>
  
  <pre><code>app.factory('ApiFactory', ['$resource', function($resource) {
      return $resource( 'http://localhost:8080/rest/forum/categories/:categoryId', { bookId: '@categoryId' }, { });
  }]);
  
  
  app.factory('ApiParser', ['ApiFactory', function(ApiFactory) {
  
      function createSubjectArray(children){
          var subjets = [],
              node,
              subject, i;
          for(i = 0; i &lt; children.length ; i++){
              node = children[i];
              subject = {};
              subject.text = node.value.nameKey;
  
              if(!node.leaf){
                  subject.items = createSubjectArray(node.children);
              } else {
                  subject.spriteCssClass = "subject";
              }
              subjets.push(subject);
          }       
          return subjets; 
      }
  
  
      return {
          get: function() {
              var root
              var data = ApiFactory.get({categoryId : 1},function(data) {
                  root = { text: data.value.nameKey, expanded: true, items: createSubjectArray(data.children)};
  
              });
  
              return root;
          }
      }
  }]);
  </code></pre>