Ionic 3 Wait for storage promise to completely finish before running function

Rendell Lasola

I’m trying to get data from the server using token from ionic storage. The problem I’m experiencing is when the get token promise can’t retrieve the token on time. Therefore, whenever I reload or reopen the app it sometimes return with an unauthorized error.

dashboard-service.ts

authUrl = "https://sampleapi.herokuapp.com"
authHeaders;

getToken() {
this.storage.get('token').then((token) => {
  console.log('Bearer ' + token);
  this.authHeaders = {
    headers: new HttpHeaders({
      'Accept': 'application/json',
      'Content-Type': 'application/json',
      'Authorization': 'Bearer ' + token
    })
}
});
}

getInfo(): Observable<Info> {
return this.http.get<Info>(this.authUrl + '/api/users/info', this.authHeaders).pipe(
  catchError(this.handleError)
);
}

dashboard.ts

ionViewDidLoad() {
this._dashboardService.getToken();
this._dashboardService.getInfo().subscribe(
  info => {
    console.log('USER INFO: ' + info);
    this.info = info
  },
  error => {
    console.log('INFO ERROR: ' + error);
  }
);
}
Melchia

You can return a promise from getToken then execute getInfo

getToken() {
return this.storage.get('token').then((token) => {
  console.log('Bearer ' + token);
  this.authHeaders = {
    headers: new HttpHeaders({
      'Accept': 'application/json',
      'Content-Type': 'application/json',
      'Authorization': 'Bearer ' + token
    })
}
});
}

In your page

 ionViewDidLoad() {
    this._dashboardService.getToken().then(_=> {
    this._dashboardService.getInfo().subscribe(
      info => {
        console.log('USER INFO: ' + info);
        this.info = info
      },
      error => {
        console.log('INFO ERROR: ' + error);
      }
    )
}
)
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Wait for function to finish running before iterating

How do I wait for a promise to finish before returning the variable of a function?

How to wait for a forEach to finish before returning from my promise / function

How to wait for a jQuery toggle to finish before running another function?

Wait for multiple http requests to finish before running a function in angular

How to wait for a method to finish completely before proceeding?

Wait a Promise to finish in an express function

Using a promise to wait for function called and finish doing what it suppose before continue

How can I use a javascript promise to make one function wait for another to finish running asynchronously?

Wait for function to finish before calling it again

Wait for a function to finish before continuing in javascript

wait for for loop to finish before executing next function

How to wait for Function to finish before continuing

Wait for one function to finish before continuing?

Jest: Wait for an async test to finish before running the next one

Wait for download method to finish before running next .then - node.js

How to wait for a loop to finish running before sending back a response in NodeJs?

Javascript/Node/Express: res.json needs to wait for a function to finish running before returning... but res.json is impatient?

Wait for All Fades in a Function to Finish Before the Next Function Starts

How to wait for function finish before starting with next function?

Typescript - Wait for promise resolve before function return

Wait for firebase fetch to finish before calling another function

React.js, wait for setState to finish before triggering a function?

React Hooks - useReducer: Wait for reducer to finish before triggering a function

How to wait for function to finish before continuning in Node.js

How to wait for command line activity to finish before calling another function?

wait for async function in loop to finish executing before next iteration

Node.js need to wait for for loop to finish before executing function

Wait for mysql to finish queries in a for loop before finishing a function in nodeJS