Typescript/Angular 2中的异步函数

Asynchronous functions in Typescript/Angular 2

本文关键字:异步 函数 Angular Typescript      更新时间:2023-09-26

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

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

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

我是angular和javascript的新手,所以请在你的解释中彻底。

干杯!

使用promise

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

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