Подождите, пока Localalstorage, а затем позвоните API - PullRequest
0 голосов
/ 30 мая 2019

Я получаю настройки от пользовательских данных и сохраняю их в местном хранилище. Я использую эти данные в вызове API.

 userlocation;
 userservice ;
 usercategory ;

 constructor(public http: HttpClient, public storage : Storage) { }

 getListings(){

 this.storage.get('area').then((userlocation) => {
  this.userlocation =  userlocation;

  console.log(this.userlocation)
 });

  this.storage.get('ser').then((userservice) => {
  this.userservice =  userservice;

  console.log(this.userlocation)
 });

  return  this.http.get(this.api_url+"?location="+ this.userlocation+ 
  "&ser= " + this.userservice)

 }

проблема в том, что вызов API происходит до того, как localalstorage получит данные. Как я могу сделать вызов API ждать для localalstorage

1 Ответ

0 голосов
/ 30 мая 2019

Используя async и await, вы можете решить эту проблему.

userlocation;
 userservice ;
 usercategory ;

 constructor(public http: HttpClient, public storage : Storage) { }

 async getListings(){ 

 await this.storage.get('area').then((userlocation) => {
  this.userlocation =  userlocation;

  console.log(this.userlocation);

    await this.storage.get('ser').then((userservice) => {
      this.userservice =  userservice;

      console.log(this.userlocation)
    });
 });



  return  this.http.get(this.api_url+"?location="+ this.userlocation+ 
  "&ser= " + this.userservice)

 }
...