title: Weird behavior when using Angular Material 2 table (Angular 2/Spring Boot backend)
body: <p>Continue to the <a href="https://stackoverflow.com/questions/45618354/using-angular-material-2-table-to-display-the-result-from-backend-based-on-user">Using angular material 2 table to display the result from backend based on user&#39;s current location</a></p>
  
  <p>My purpose for this code is when user enter the site, it will try to ask user the current location. Once my front end get current lat/lon, it will pass to backend to get the nearest restaurant based on user's location, and using angular material table to display it. But when I testing on Chrome, I got weird behavior, the home page will not display the result immediately on the first time, try refresh, doesn't work, the only way make it works is switch another tab, and back to this one, it will display the result in angular material table.</p>
  
  <p>Here is the code for home.component.ts</p>
  
  <pre><code>import { Component, OnInit } from '@angular/core';
  import { Http, Response, URLSearchParams } from '@angular/http';
  import { DataSource } from '@angular/cdk';
  import { BehaviorSubject } from 'rxjs/BehaviorSubject';
  import { Observable } from 'rxjs/Observable';
  import 'rxjs/add/operator/startWith';
  import 'rxjs/add/observable/merge';
  import 'rxjs/add/operator/map';
  import 'rxjs/add/observable/frompromise';
  import { Restaurant } from '../restaurant/restaurant';
  import { Category } from '../category/category';
  import { RestaurantService } from '../restaurant/restaurant.service';
  
  
  @Component({
    selector: 'app-home',
    templateUrl: './home.component.html',
    styleUrls: ['./home.component.css']
  })
  export class HomeComponent implements OnInit {
    displayedColumns = ['Id', 'Name', 'Category', 'Address', 'City'];
    dataSource: ExampleDataSource | null;
  
    constructor(http: Http) {
      //this.exampleDatabase = new ExampleHttpDatabase(http, this.location);
      this.dataSource = new ExampleDataSource(http);
  
    }
  
    ngOnInit() {
      this.dataSource.connect();
    }
  }
  
  export class ExampleDataSource extends DataSource&lt;Restaurant&gt; {
    private url = 'api/search/location';
    private params = new URLSearchParams();
    private lat;
    private lon;
    constructor(private http: Http) {
      super();
    }
  
    /** Connect function called by the table to retrieve one stream containing the data to render. */
    connect(): Observable&lt;Restaurant[]&gt; {
      // var location;
      // if (navigator.geolocation){
      //   var options = {timeout: 60000};
      //   location = navigator.geolocation.getCurrentPosition((position)=&gt;{
      //     return position;
      //   },(err) =&gt;{
      //     console.log("Error")
      //   }, options);
      // }
      // console.log("Locations: " + location);
      var result = this.getCurrentLocation().then((res) =&gt; 
      {
        return res;
      });
      return Observable.fromPromise(result);
    }
  
  
    disconnect() { }
    getPosition = () =&gt; {
      var latitude, longitude;
      return new Promise((resolve, reject) =&gt; {
        navigator.geolocation.getCurrentPosition((position) =&gt; {
          resolve(position.coords);
        }, (err) =&gt; {
          reject(err);
        });
      })
    }
    async getCurrentLocation(): Promise&lt;Restaurant[]&gt; {
      let coords = await this.getPosition();
      this.lat = coords['latitude'];
      this.lon = coords['longitude'];
      this.params.set('lat', this.lat);
      this.params.set('lon', this.lon);
      var result = this.http.get(this.url, { search: this.params }).map(this.extractData);
      return await result.toPromise();
    }
    extractData(result: Response): Restaurant[] {
      return result.json().map(restaurant =&gt; {
        return {
          id: restaurant.id,
          name: restaurant.restaurant_name,
          category: restaurant.category.map(c =&gt; c.categoryName).join(','),
          address: restaurant.address.address,
          city: restaurant.address.city.cityName
        }
      });
    }
  }
  </code></pre>
  
  <p>I don't know what I did wrong.. can someone help me? For the full code, please see <a href="https://github.com/zhengye1/Eatr/tree/dev" rel="nofollow noreferrer">https://github.com/zhengye1/Eatr/tree/dev</a></p>