Angular 呼叫服务完成后的呼叫功能

Angular 呼叫服务完成后的呼叫功能,angular,typescript,rxjs,Angular,Typescript,Rxjs,我有一个调用服务的函数 private callService() { this.functionOne(); this.functionTwo(); this.functionThree(); } private getOtherInfo() { // pure sync here this.getId(this.user['data']); this.getType(this.name['data']); } 我希望执行顺序是先运行callServic

我有一个调用服务的函数

private callService() {
   this.functionOne();
   this.functionTwo();
   this.functionThree();
}

private getOtherInfo() {
   // pure sync here
   this.getId(this.user['data']);
   this.getType(this.name['data']);
}
我希望执行顺序是先运行
callService
,然后运行
getOtherInfo
。 但是我发现代码无法达到第二个函数

callService
中的函数在某种程度上类似于

private functionOne() {
    this.user['loading'] = true;
    this.service['user'].get().subscribe(data => {
    this.user['data'] = data;
   }
}

private functionTwo() {
    this.name['loading'] = true;
    this.service['name'].get().subscribe(data => {
    this.name['data'] = data;
   }
}
.....
所以我把函数改为

private callService(): Promise<any> {
     return Promise.resolve() => {
      this.functionOne();
      this.functionTwo();
      this.functionThree();
     });
} 

但是,第二个函数仍然没有达到。

代码看起来不正确<代码>功能一,
功能二
,等等。。。实际上并没有兑现承诺。事实上,他们根本没有归还任何东西。你有
.subscribe
,但你从来没有用它做过任何事情。如果您将函数包装在承诺中并正确解决它们,您应该能够毫无问题地等待所有函数:

例如:

private functionOne() {
    return new Promise( (resolve, reject) => {
        this.user['loading'] = true;
        this.service['user'].get().subscribe(data => {
        this.user['data'] = data;
           resolve();
        });
    });
}

private functionTwo() {
    return new Promise( (resolve, reject) => {
        this.name['loading'] = true;
        this.service['name'].get().subscribe(data => {
            this.name['data'] = data;
            resolve();
        });
    });
}

private callService(): Promise<any> {
     return new Promise( async (resolve, reject) => {
      await this.functionOne();
      await this.functionTwo();
      await this.functionThree();
     });
});

代码看起来不正确<代码>功能一,
功能二
,等等。。。实际上并没有兑现承诺。事实上,他们根本没有归还任何东西。你有
.subscribe
,但你从来没有用它做过任何事情。如果您将函数包装在承诺中并正确解决它们,您应该能够毫无问题地等待所有函数:

例如:

private functionOne() {
    return new Promise( (resolve, reject) => {
        this.user['loading'] = true;
        this.service['user'].get().subscribe(data => {
        this.user['data'] = data;
           resolve();
        });
    });
}

private functionTwo() {
    return new Promise( (resolve, reject) => {
        this.name['loading'] = true;
        this.service['name'].get().subscribe(data => {
            this.name['data'] = data;
            resolve();
        });
    });
}

private callService(): Promise<any> {
     return new Promise( async (resolve, reject) => {
      await this.functionOne();
      await this.functionTwo();
      await this.functionThree();
     });
});

所有这些都可以用可观测数据来解决,而无需承诺

您需要将三个函数更改为:

private functionOne() {
    this.user['loading'] = true;
    return this.service['user'].get();
}

private functionTwo() {
    this.name['loading'] = true;
    return this.service['name'].get();
}
private callService(): Promise<any> {
     forkJoin(
      this.functionOne(),
      this.functionTwo(),
      this.functionThree()
     ).subscribe(([user, name, fnThreeData]) => {
       this.user['data'] = user;
       this.name['data'] = name;
       //whatever with fnThreeData
       this.getOtherInfo();
     });
} 
然后您的呼叫服务将是:

private functionOne() {
    this.user['loading'] = true;
    return this.service['user'].get();
}

private functionTwo() {
    this.name['loading'] = true;
    return this.service['name'].get();
}
private callService(): Promise<any> {
     forkJoin(
      this.functionOne(),
      this.functionTwo(),
      this.functionThree()
     ).subscribe(([user, name, fnThreeData]) => {
       this.user['data'] = user;
       this.name['data'] = name;
       //whatever with fnThreeData
       this.getOtherInfo();
     });
} 
private callService():承诺{
分叉连接(
this.functionOne(),
这个.functionTwo(),
这是第三个功能()
).subscribe(([用户、名称、数据])=>{
this.user['data']=用户;
this.name['data']=name;
//不管是什么数据
这是getOtherInfo();
});
} 

不要试图将承诺和其他东西混为一谈,rxjs提供了您需要的所有功能。如果你不购买rxjs,你将在angular面临一场艰苦的战斗。

所有这一切都可以用可观测数据解决,而无需承诺

您需要将三个函数更改为:

private functionOne() {
    this.user['loading'] = true;
    return this.service['user'].get();
}

private functionTwo() {
    this.name['loading'] = true;
    return this.service['name'].get();
}
private callService(): Promise<any> {
     forkJoin(
      this.functionOne(),
      this.functionTwo(),
      this.functionThree()
     ).subscribe(([user, name, fnThreeData]) => {
       this.user['data'] = user;
       this.name['data'] = name;
       //whatever with fnThreeData
       this.getOtherInfo();
     });
} 
然后您的呼叫服务将是:

private functionOne() {
    this.user['loading'] = true;
    return this.service['user'].get();
}

private functionTwo() {
    this.name['loading'] = true;
    return this.service['name'].get();
}
private callService(): Promise<any> {
     forkJoin(
      this.functionOne(),
      this.functionTwo(),
      this.functionThree()
     ).subscribe(([user, name, fnThreeData]) => {
       this.user['data'] = user;
       this.name['data'] = name;
       //whatever with fnThreeData
       this.getOtherInfo();
     });
} 
private callService():承诺{
分叉连接(
this.functionOne(),
这个.functionTwo(),
这是第三个功能()
).subscribe(([用户、名称、数据])=>{
this.user['data']=用户;
this.name['data']=name;
//不管是什么数据
这是getOtherInfo();
});
} 


不要试图将承诺和其他东西混为一谈,rxjs提供了您需要的所有功能。如果你不购买rxjs,你将在angular面临一场艰苦的战斗。

在angular中使用承诺绝对没有问题。零。是否使用rxjs是个人偏好。我承认,rxjs在昂贵的自动完成查找(即:搜索)方面确实有一些很好的功能。我更喜欢承诺的语法,现在它们更标准了。很多库都在返回承诺(比库返回rxjs可观测值更重要),angular开发团队和我曾经合作过的每个angular项目都分享了我的观点。RXJS是几个核心服务的基础,与框架紧密交织在一起。除此之外,RXJS还解决了许多缺点(延迟加载、多重排放等)。在简单的应用程序中,承诺可能会起作用,但在更复杂的环境中,您会很高兴切换到RXJS。还有,承诺是一个垂死的标准。他们是大人物,但他们正在被RXJS取代你什么意思?我是说我们有下一个。我还希望OneError和onCompleted记录简单的消息。感谢您的帮助。如果您想在函数中单独执行此操作,请分别使用catchError和finalize运算符,总体而言,您可以使用subscribe的错误和完整处理程序。对于简单的日志记录,使用tap Operator在angular中使用Promission绝对没有问题。零。是否使用rxjs是个人偏好。我承认,rxjs在昂贵的自动完成查找(即:搜索)方面确实有一些很好的功能。我更喜欢承诺的语法,现在它们更标准了。很多库都在返回承诺(比库返回rxjs可观测值更重要),angular开发团队和我曾经合作过的每个angular项目都分享了我的观点。RXJS是几个核心服务的基础,与框架紧密交织在一起。除此之外,RXJS还解决了许多缺点(延迟加载、多重排放等)。在简单的应用程序中,承诺可能会起作用,但在更复杂的环境中,您会很高兴切换到RXJS。还有,承诺是一个垂死的标准。他们是大人物,但他们正在被RXJS取代你什么意思?我是说我们有下一个。我还希望OneError和onCompleted记录简单的消息。感谢您的帮助。如果您想在函数中单独执行此操作,请分别使用catchError和finalize运算符,总体而言,您可以使用subscribe的错误和完整处理程序。对于简单的日志记录,请使用tap运算符将PROMITE和Async混合在一起。很难理解,实际上它不起作用不知道为什么?异步只是一种标记函数的方法,以便可以利用wait。在函数中返回承诺会自动使函数成为可等待的。好的。我想这就是代码不起作用的原因。假设我有三个功能。它们两个是异步的。第三个是同步的。我怎样才能把它们连在一起?第三个需要来自前两个函数的数据。我在谷歌上搜索了很多。似乎都在谈论异步方法。我提供的第一个块可能不起作用,因为这是一个如何实现的示例。您问题中提供的代码不完整,因此如果没有缺失的代码,我无法使其正常工作,这就是为什么我提供了第二段代码来向您展示如何从头开始