【问题标题】:Asynchronous functions in Typescript/Angular 2Typescript/Angular 2 中的异步函数
【发布时间】:2016-11-13 05:27:38
【问题描述】:

嵌套两个函数时遇到问题。第二个函数在第一个函数完成之前执行。 我有两种方法:

doLogin() {
    return this.authService.doLogin();
}
 toLogin(){
    this.router.navigateByUrl("/secure");
}

第一个函数 doLogin() 由于服务的原因需要一段时间。如何使第二个函数 toLogin() 仅在 doLogin() 完成并返回 true 后执行(使用承诺或回调)?

我是 Angular 和 javascript 的新手,所以请详细解释一下。

干杯!

【问题讨论】:

    标签: javascript angularjs asynchronous promise es6-promise


    【解决方案1】:

    通过使用承诺

    doLogin() {
        return this.authService.doLogin().then(function(result){
            toLogin();
        });
    }
    

    你需要在 this.authService.doLogin() 中返回一个 promise

    【讨论】:

      猜你喜欢
      • 2017-12-11
      • 2022-11-04
      • 1970-01-01
      • 2016-06-15
      • 2019-10-02
      • 1970-01-01
      • 1970-01-01
      • 2017-12-20
      • 1970-01-01
      相关资源
      最近更新 更多