打字稿中函数的异步行为 [英] asynchronous behavior of functions in typescript

查看:73
本文介绍了打字稿中函数的异步行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了以下代码段:

var tmpString = null;
    this.restProvider.validateUser(this.registerCredentials.email, this.registerCredentials.password)
      .then(data => {
        tmpString = JSON.stringify(data);
      });
    console.log(tmpString);

但是,尽管数据不为null,但控制台会在短时间内将输出null值并将tmpString设置为正确的值.我该如何解决这个问题?谢谢

But, despite data is not null, console print a null value and tmpString is set to the right value after a short time. How I can solve that problem? Thanks

真正的功能是:

registerNewUser()
  {
    var tmpString = null;
    this.restProvider.validateUser(this.registerCredentials.email, this.registerCredentials.password)
      .then(data => {
        tmpString = JSON.stringify(data);
      });
    console.log(tmpString);
    if(tmpString == "false")
    {
      return false;
    }
    else
    {
      this.registerCredentials.email = JSON.parse(tmpString).email;
      this.registerCredentials.password = JSON.parse(tmpString).password;
      this.email = JSON.parse(tmpString).email;
      this.password = JSON.parse(tmpString).password;
    }
    return this.email + this.password;
  }

我用它

public login() {
    this.showLoading();
    this.registerNewUser();

    if(this.email == "false" && this.password == "false")
    {
      this.showError("Access Denied");
    }
    else
    {
      this.auth.login(this.registerCredentials);
      this.showError("Access Permit");

    }
}

我决心这样做

registerNewUser()
  {
    var tmpString = null;
    this.restProvider.validateUser(this.registerCredentials.email, this.registerCredentials.password)
      .then(data => {
        tmpString = JSON.stringify(data);
        if(tmpString == "false")
        {
          this.showError("Access Denied");
        }
        else
        {
          this.registerCredentials.email = JSON.parse(tmpString).email;
          this.registerCredentials.password = JSON.parse(tmpString).password;
          this.email = JSON.parse(tmpString).email;
          this.password = JSON.parse(tmpString).password;

          this.auth.login(this.registerCredentials);
          this.showError("Access Permit");
        }
      });

  }

但这是正确的解决方案吗?

But is this the right solution?

推荐答案

考虑以下代码(带有行号的注释):

Taking into account the below code (commented with line numbers):

var tmpString = null; // 1
this.restProvider.validateUser(this.registerCredentials.email, this.registerCredentials.password)
  .then(data => {
    tmpString = JSON.stringify(data); // 2
  });
console.log(tmpString); // 3

执行顺序为:

  1. 第1行
  2. 第3行
  3. 第2行

这是因为到达第3行时,异步请求尚未完成.因此,为了正确打印 tmpString ,请在回调的第2行之后(响应到达时)将 console.log(tmpString); 移动,如下所示:

That's because when line 3 is reached, the asynchronous request hasn't finished. So in order to print tmpString properly, move the console.log(tmpString); after line 2, inside the callback (when the response arrived), like this:

var tmpString = null; // 1
this.restProvider.validateUser(this.registerCredentials.email, this.registerCredentials.password)
  .then(data => {
    tmpString = JSON.stringify(data); // 2
    console.log(tmpString); // 3
  });

这篇关于打字稿中函数的异步行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆