title: What is the best way to interact with Indexed db when getting and setting values using promises?
body: <p>I'm using <strong>LocalForage library</strong> to have offline data in my web app.</p>
  
  <p>I have a separate service to get and set values to indexed db.The service as follows,</p>
  
  <pre><code>var service = angular.module("DBService",[]);
  
  service.factory('$dbService', function($q){
  // Configuration
  drivers = [localforage.INDEXEDDB, localforage.LOCALSTORAGE];
  localforage.config({
      driver: drivers,
      name:"Cars"
  });
  localforage.setDriver(localforage.INDEXEDDB);
  //
  var _set = function(key_,value_){
      //
      var defered = $q.defer();
      localforage.setItem(key_, value_).then(
          function(data){
              //
              defered.resolve(data);
          }
      );
      return defered.promise;
  };
  var _get = function(key_){
      var defered = $q.defer();
      localforage.getItem(key_).then(
          function(data){
              //
              defered.resolve(data);
          }
      );
      return defered.promise; 
  };
  
  return {
      _set: _set,
      _get: _get
  }
  </code></pre>
  
  <p>});</p>
  
  <p>Following is the view,</p>
  
  <pre><code>&lt;html ng-app="App"&gt;
  &lt;head&gt;
  &lt;script src="angular.min.js"&gt;&lt;/script&gt;
  &lt;script src="localforage.js"&gt;&lt;/script&gt;
  &lt;script src="service.js"&gt;&lt;/script&gt;
  &lt;script src="main.js"&gt;&lt;/script&gt;
  &lt;title&gt;Angular App&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div ng-controller="DatabaseController" id="main" ng-init="pageInit()"&gt;
      &lt;button ng-click="saveAsync(4)"&gt;click&lt;/button&gt;
      &lt;button ng-click="saveAsync2(5)"&gt;click&lt;/button&gt;
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Following is the controller,</p>
  
  <pre><code>var app = angular.module("App",['DBService']);
  
  app.controller('DatabaseController', function($scope, $dbService){
  //
  $scope.pageInit = function(){
      //
      var initialize = [1,2,3];
      $dbService._set('List', initialize).then(
          function(data){
              //
              console.log("init : " + JSON.stringify(data));
          }
      );
  };
  //
  $scope.$saveAsync = function(value_){
      //
      $dbService._get('List').then(
          function(data){
              //
              console.log("async operation : " + JSON.stringify(data));
              var tempList = data;
              tempList.push(value_);  
              $dbService._set('List',tempList).then(
                  function(data){
                      //
                      console.log("async operation resolved : " +  data);
                  }
              );
          }
      );
  };
  //
  $scope.saveAsync = function(value_){
      //
      $scope.$saveAsync(value_);
  };
  //
  $scope.saveAsync2 = function(value_){
      //
      $scope.$saveAsync(value_);
  };
  //
  setInterval(function() {
      $scope.$saveAsync(6);
      $scope.$saveAsync(7);
  }, 1000);
  });
  </code></pre>
  
  <p>The problem occurs at the setInterval as it will call $scope.$saveAsync() method twice with different parameters. But at the point of resolving the both promises they only contains the initial value which is 1,2,3 according to this scenario.
  so the program will set 1,2,3,6 in the first $scope.$saveAsync() method call
  and 1,2,3,7 in the second $scope.$saveAsync() method call. Finally only 1,2,3,7 will be get saved in indexeddb the value 6 is missed.</p>
  
  <p>having a $q.all resolves this problem. but it is only applicable within the setInterval scope. As the user can have read write actions to the same data at the point of setInterval also running pointing to the same data.</p>
  
  <p>Likewise The problem occurs when i tries to get a particular key value for a several times simultaneously and tries to update that key's values. 
  Because calling to the method get() some times returns an old data set as the get calls will be asynchronous. so when the set method is invoked at an old data set there is a data loose within the indexed db. </p>
  
  <p>Thanks in advice.</p>