简体   繁体   English

在Ionic3和angular4中使用Async和Await

[英]Using Async and Await in Ionic3 and angular4

we never used await/async, so the question could be also a stupid one but what do we wrong? 我们从来没有使用过await / async,所以这个问题可能也是一个愚蠢的问题,但是我们错了什么呢?

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: TEEEEXXTTTTT但类似的东西

text: ZoneAwarePromise {__zone_symbol__state: null, __zone_symbol__value: Array[0]}

I'm sure there is a simple solution :smile: 我敢肯定有一个简单的解决方案:smile:

thx 谢谢

this.textAsync(); returns a promise, so you have to wait until it's resolved: 返回一个promise,因此您必须等到解决为止:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM