title: Handling a promise in a Ionic/Cordova Application
body: <p>I'm currently working on a <code>Ionic/Cordova</code> application, a to-do application. I am using the <code>ngCordova</code> library for an easier use of the cordova plugins.  I'm also using the <code>Sqlite</code> plugin by <code>litehelpers</code> (<a href="https://github.com/litehelpers/Cordova-sqlite-storage" rel="nofollow">GitHub page</a>).</p>
  
  <p>My problem is about the correct understanding of the JavaScript's promises behavior. I have a Angular service called "Projects", which make use of the SQLite plugin, and a "Tasks" controller which is delegated to it.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>angular.module('myapp.services', [])
  
  .factory("Projects", ["$ionicPlatform", "$cordovaSQLite", "$window", "$q",
    function($ionicPlatform, $cordovaSQLite, $window, $q) {
      return {
        // SOME FUNCTIONS..
        getCurrentProject: function() {
          var q = $q.defer();
  
          $ionicPlatform.ready(function() {
            $cordovaSQLite.execute(db, 
                "SELECT id_project, name FROM projects WHERE active = 1").then(
                function(res) {
  
                q.resolve(res.rows.item(0));
            }, function(err) {
              q.reject(err);
              console.error(err.message);
            });
          });
  
          return q.promise;
        }
      };
    }
  ]);</code></pre>
  </div>
  </div>
  </p>
  
  <p>I want to make this function return an object, which is the result of my query. </p>
  
  <p>The <code>promises</code> and <code>callback</code> keywords are confusing me.</p>
  
  <p>I've tried a few ways to solve my problem, but when I call (in my controller):</p>
  
  <pre><code>var currentProject = Projects.getCurrentProject();
  </code></pre>
  
  <p>And then trying to check its values with:</p>
  
  <pre><code>console.log("ID -&gt; " + currentProject.id_project);
  console.log("NAME -&gt; " + currentProject.name);
  </code></pre>
  
  <p>I always get <code>undefined</code>.  How do I handle a <code>promise</code> in a Ionic/Cordova Application?</p>
  
  <h2>Solved</h2>
  
  <p>@bardzusny's answer totally made me realize where i was wrong, thank you all :)</p>