title: React Native Async Await in react-native-sqlite-storage
body: <p>I am little new to React native. I have gone through lot of tutorials before starting up. I am trying to make stores in my app which can be accessed by Screens of the app when required.</p>
  
  <p>Here is my file structure.</p>
  
  <ol>
  <li>index.ios.js</li>
  <li>/app/index.js</li>
  <li>/app/store/database.js</li>
  <li>/app/store/userStore.js</li>
  </ol>
  
  <p><strong>index.ios.js</strong></p>
  
  <pre><code>import { AppRegistry } from 'react-native';
  import App from './app/index';
  AppRegistry.registerComponent('AwesomeProject', () =&gt; App);
  </code></pre>
  
  <p><strong>/app/index.js</strong></p>
  
  <pre><code>import React, { Component} from 'react';
  import {
    Text,
    View,
  } from 'react-native';
  import userStore from './store/userStore';
  import ViewBackground from './components/ViewBackground';
  
  
  class App extends Component {
          constructor(props){
              super(props);
              this.isLoggedIn = true;     
          }
  
          componentDidMount() {       
              this.fetchUsers().done();
          }
  
          async fetchUsers(){
              console.log('Before User Fetch');
              var result = await userStore.getAllUsers();
              console.log('After User Fetch');
          }
  
          render() {
              if(this.isLoggedIn){
                  return this.loggedInView();
              }else{
                  return this.loggedOutView();
              }
          }
  
          loggedInView(){
              return &lt;ViewBackground&gt;
  
              &lt;/ViewBackground&gt;
          }
  
          loggedOutView(){
              return &lt;View&gt;
              &lt;Text&gt;Hi, This is logged Out view &lt;/Text&gt;
              &lt;/View&gt;
          }
      }
  
      export default App;
  </code></pre>
  
  <p><strong>/app/store/userStore.js</strong></p>
  
  <p>import React from 'react';
          import database from './database';</p>
  
  <pre><code>    class UserStore{
  
          async getAllUsers(){
              let query = {
                  type: 'SELECT',
                  sql: 'SELECT * FROM Users',
                  vals: [],
              }
              let queries = [query];
              console.log('in store before');
              let dbResult = await database.runQuery(queries);        
              console.log('in store after');
              return dbResult;        
          }
      }
  
      const userStore = new UserStore;
  
      export default userStore;
  </code></pre>
  
  <p><strong>/app/store/database.js</strong></p>
  
  <pre><code>    'use strict';
      import React from 'react';
      import SQLite from 'react-native-sqlite-storage';
      SQLite.enablePromise(true);
  
      var database_name = "test.db";
      var database_version = "1.0";
      var database_displayname = "Test";
      var database_size = 1024 * 1024 * 10;
  
      class Database  {
  
          constructor(){
              SQLite.openDatabase(database_name, database_version, database_displayname, database_size).then(this.dbOpenSuccess).catch(this.dbOpenError);
          }
  
          dbOpenSuccess(dbInstance){
              this.conn = dbInstance;
          }
  
          dbOpenError(err) {
  
          }
  
          getConnection() {
              return this.conn;
          }
  
          setUpDatabase(){
              let queries = [
              'CREATE TABLE IF NOT EXISTS Users (user_id INTEGER PRIMARY KEY, f_name VARCHAR(64), l_name VARCHAR(64), email_id VARCHAR(128), mobile VARCHAR(10))'
              ];
              this.conn.transaction( tx =&gt; {
                  queries.map( q =&gt; {
                      tx.executeSql(q, [], res =&gt; {}, err =&gt; {});
                  });
              });
          }
  
          async runQuery(queries) {
              await this.conn.transaction(tx =&gt; {
                  return Promise.all(queries.map(async (q) =&gt; {
                      try {
                          let results = null;
                          switch(q.type){
                              case 'SELECT':                      
                              results = await tx.executeSql(q.sql, q.vals);
                              console.log('Query', q, 'Executed. results:', results);
                              break;
                          }
  
                      } catch(err) {
                          console.log('Something went wrong while executing query', q, 'error is', err);
                      }
                  }));
              });
              return false;
          }
  
          closeDatabase(){
              this.conn.close().then(this.dbCloseSuccess).catch(this.dbCloseError);
          }
  
          dbCloseSuccess(res) {
  
          }
  
          dbCloseError(err){
  
          }
      }
  
      const dbInstance = new Database();
  
      dbInstance.setUpDatabase();
  
      export default dbInstance;
  </code></pre>
  
  <p>As you can see from the code that in <strong>/app/index.js</strong>, I am trying to fetch the users from the <strong>userStore.js</strong> which will finally call the <strong>database.js</strong> file to retrieve the users from the database. I want to pause the execution of the code when I am trying to fetch the users from DB, so I have tried to use async/await. </p>
  
  <p>If you can see in the code I have used <strong>console.log()</strong> to check whether the execution pause or it continues. None of the async/await is working in any of the file. I am getting response <strong>after all the console.log</strong> is done.</p>
  
  <p>I want to wait for the time till I receive the data from the DB. Please give some expert advise, where I am going wrong. Thanks in advance.</p>