javascript - 如何在 Typescript 中依次执行 2 个函数?
问题描述
我正在调用以下两个函数:
getStudentDataFunction(){
........
return studentData
}
getTeachersDataFunction(){
........
return teachersData
}
我希望在 getStudentDataFunction() 完成返回数据后执行 getTeachersDataFunction()。
有没有办法做到这一点?
解决方案
您可能需要使用 Rxjs 使这两个方法可观察,因此您可以执行以下操作:
caller() {
this.method1()
.pipe(switchMap(res1 => this.method2(res1)))
.subscribe(res2 => {
console.log(res2);
});
}
另请参阅演示:https ://stackblitz.com/edit/angular-ivy-rn5kfe?file=src/app/app.component.ts
推荐阅读
- python - Python MemoryError 未引发 - 改为调用 OOM-Killer?
- python - 在python中递归更改布尔值
- matlab - 在 Matlab 中多次有效地求解同一个矩阵?
- android - 这里 Android SDK - 如何构建示例应用程序?
- arrays - Swift数组按字母顺序和组顺序排序
- azure - 如何使用 LoadBalancer 服务从 Azure Kubernetes 内部获取客户端 IP 地址
- c++ - Boost Chrono Header Only 与 Linked
- mongodb - MongoDB聚合,如何返回未受影响的项目
- python - 如何将列表列表更改为字典字典?
- c++ - CMake 包含非 cmake 库的第三方标头