ionic storage getting values in Aync way

前端 未结 1 1849
情深已故
情深已故 2021-01-16 17:10

I am trying to get 2 values stores in ionic storage , but the value are retrived in async way and request is happening before the values are retrived

Here Auth, url

相关标签:
1条回答
  • 2021-01-16 17:47

    You have to wait for both promise to get resolved before make request. First move your storage code to some init method.

    public init(){
    
    let promiseList: Promise<any>[] = [];
    promiseList.push(
     this.storage.get('Auth').then((Auth) => {
          console.log('Retrived Auth is', Auth);
          this.Auth = Auth;
    
        } ));
    promiseList.push(
        this.storage.get('url').then((url) => {
          console.log('Retrived url is', url);
          this.url = url;
        } ));
    
    return Promise.all(promiseList);
    }
    

    Now call init method before getSesonList as followed:

    this.sessionService.init().then((values)=>{
    this.sessionService.getSeasonList();
    });
    

    this will make sure that the getSeasonList method will be called after both storage promises get resolved.

    Obviously you should put some error handling code but its upon you.

    0 讨论(0)
提交回复
热议问题