Typescript/Angular 2 中的异步函数

Asynchronous functions in Typescript/Angular 2

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

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

第一个函数 doLogin() 由于服务需要花费一些时间。如何使第二个函数 toLogin() 仅在 doLogin() 完成且 returns 为真(使用承诺或回调)后执行?

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

干杯!

通过使用承诺

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

您需要 return 在 this.authService.doLogin()

中做出承诺