Using Async and Await in Ionic


#1

Hi to every one, we never used await/async, so the question could be also a stupid one but what do we wrong?

This is our code:

  constructor() {
       let text = this.textAsync();
       console.log('text: ', text);
  }
  
  
  async textAsync() {
    let val = await this._text(); 
    return val;
  }
  
  _text(): Promise<any> {
    return new Promise((resolve, reject) => {
           resolve('TEEEEXXTTTTT');
    })
  }

The log is never text: TEEEEXXTTTTT but something like that
text: ZoneAwarePromise {__zone_symbol__state: null, __zone_symbol__value: Array[0]}

I’m sure there is a simple solution :smile:

thx


#2

Does someone have an idea???
thx


#3

You console.log the promise, not the result.
Maybe that will work better:

constructor() {
       let text = this.textAsync().then( text =>
         console.log('text: ', text)
       );     
  }

#4

And proves you don’t need await async to work with async stuff, as imho proper use of promises and observables is much cleaner