title: Implementing promises and AsyncStorage in react-native
body: <p><strong>What I have</strong></p>
  
  <p>2 functions-</p>
  
  <ol>
  <li><p>getListInfo - here I am checking if an object exists, if not, create it, then, return it.</p></li>
  <li><p>getInitialState - default function of react native.</p></li>
  </ol>
  
  <p><strong>What I want to achieve</strong></p>
  
  <p>Try to call getListInfo from getInitialState, and eventually fill the listview.</p>
  
  <p><strong>But, what is happening-</strong></p>
  
  <p>The code of whole page is getting executed even before the value obj is being returned. So, the problem is, the List View is getting the data as undefined.</p>
  
  <p><strong>What I know</strong></p>
  
  <p>To use promise to solve the problem. But, not the right way.</p>
  
  <p>Actual Code -</p>
  
  <pre><code>getListInfo : function() {
      AsyncStorage.getItem('listobject').then((obj) =&gt; {
      if(obj == undefined)
      {
          var obj1 ={};
          obj1.data ={};
          obj1.data.isdirty = true;
          console.log("obj1 = "+ JSON.stringify(obj1));
          AsyncStorage.setItem('listobject',obj1);
          obj = AsyncStorage.getItem('listobject');
          console.log("obj = "+ JSON.stringify(obj));
      }
      if(obj.data.isdirty)
      {
          obj.data.isdirty = false;
          AsyncStorage.setItem('listobject',JSON.stringify(obj));
          return AsyncStorage.getItem('listobject');
      }
  }).done();
  },
  
  
    getInitialState: function() {
      applistglobal = this.checkLocalStore();
  
      var ds = new ListView.DataSource({rowHasChanged: (r1, r2) =&gt; r1 !== r2});
      return {
        dataSource: ds.cloneWithRows(this._genRows({})),
      };
    },
  
  
  render: function() {
      /* BLAH BLAH BLAH */
  }
  </code></pre>
  
  <p>Promise part of the code(getInitialState function)-
  I have used promises, but, even the function call is failing. So, please ignore this part of the code and evaluate the above code.</p>
  
  <pre><code>getInitialState: function() {
      var promise = new Promise(function (resolve, reject) {
        resolve(this.getListInfo());
      });
  
      promise.then((listobject) =&gt; {
        console.log("getInitialState listobject "+listobject);
      })
  
      promise.catch((error) =&gt; {
        console.log("ERROR"+error);
      })
  
      var ds = new ListView.DataSource({rowHasChanged: (r1, r2) =&gt; r1 !== r2});
      return {
        dataSource: ds.cloneWithRows(this._genRows({})),
      };
    },
  </code></pre>