typescript 离子 3/打字稿承诺

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47767654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:06:51  来源:igfitidea点击:

Ionic 3/Typescript Promise

typescriptionic-framework

提问by Wang

Im trying to use Promise to get data from web service

我正在尝试使用 Promise 从 Web 服务获取数据

  login(){
var obj = {email:'WHATEVER', firstname:''};
var b = this.getUsers().then(data => {
  this.films = data;
  obj = this.films.customer_info;
  console.log('inside:' + obj.firstname);
});
console.log('outside:' + obj.firstname);

}

}

My expected result:

我的预期结果:

outside:Nam Nam login.ts:51 inside:Nam Nam
login.ts:48

外部:Nam Nam login.ts:51 内部:Nam Nam
login.ts:48

My actual result :

我的实际结果:

outside: login.ts:51 inside:Nam Nam
login.ts:48

外部:login.ts:51 内部:Nam Nam
login.ts:48

Can someone tell me how to solve this problem? thanks

有人能告诉我如何解决这个问题吗?谢谢

回答by Fenton

Your expected result is not possible, because you are asserting that you want a value (that will be obtained asynchronously) to be available synchronously.

您的预期结果是不可能的,因为您断言您希望一个值(将异步获得)同步可用。

Let's look into it in more detail... here is a short, but fully working example of your problem:

让我们更详细地研究一下……这是您的问题的一个简短但完全有效的示例:

class Example {
  private films: any;

  login() {
    var obj = { email: 'WHATEVER', firstname: '' };
    var b = this.getUsers().then(data => {
      this.films = data;
      obj = this.films.customer_info;
      console.log('inside:' + obj.firstname);
    });
    console.log('outside:' + obj.firstname);
  }

  getUsers() {
    return new Promise((resolve, reject) => {
      window.setTimeout(() => {
        console.log('Result obtainined... for example from a service');
        resolve({ customer_info: { firstname: 'Fenton' } });
      }, 1000);
    })
  }
}

const example = new Example();

example.login();

The output from this program is:

这个程序的输出是:

outside:
Result obtainined... for example from a service
inside:Fenton

As you can see, the statement that is outside of the thenhandler executes before the promise resolves. If you are calling out to a service, this line of code will run before the server has responded.

如您所见,then处理程序之外的语句在承诺解析之前执行。如果您正在调用服务,这行代码将在服务器响应之前运行。

That's actually the whole point of promises - which is why it works insideyour thenhandler.

这实际上是 promises 的全部意义——这就是它你的then处理程序中工作的原因。

If you have code that depends on the logincompleting, you could return a promise from the loginmethod. Based on the above example, you could use:

如果您有依赖于login完成的代码,您可以从该login方法返回一个承诺。基于上面的例子,你可以使用:

  login(): Promise<{ firstname: string}> {
    return new Promise((resolve, reject) => {
      this.getUsers().then(data => {
        this.films = data;
        resolve(this.films.customer_info);
      });
    });
  }

When the login completes, the customer info is available:

登录完成后,客户信息可用:

example.login()
    .then((customer) => console.log(customer.firstname));