简体   繁体   English

Angular-同时执行多项功能

[英]Angular - Execute multiple functions at the same time

I have 3 functions that each call another function that make an API request. 我有3个函数,每个函数都调用另一个发出API请求的函数。 All this called one at a time takes time, what I would like is that these 3 functions are called at the same time to gain time. 一次调用所有这些都需要时间,我想同时调用这3个函数以节省时间。 I tried to use mergeMap() but it doesn't work. 我尝试使用mergeMap(),但不起作用。 Nothing happens, I tried to display things in my console but it seems that the code contained in the mergeMap() is not executed 没有任何反应,我试图在控制台中显示内容,但似乎未执行mergeMap()中包含的代码

Here is my code with mergeMap(): 这是我的带有mergeMap()的代码:

from(['corps', 'disciplines', 'departements']).pipe(
  mergeMap(f => {
    switch (f) {
      case 'corps':
        this.getCorps();
        break;
      case 'disciplines':
        this.getDisciplines();
        break;
      case 'departements':
        this.getDepartements();
        break;
    }
    return undefined;
  })
);

By the way if you have an idea for deleting this switch case by replacing the first array to [this.getCorps(), this.getDisciplines(), this.getDepartements()] and execute these 3 function at the same time without the switch case it would be great ! 顺便说一句,如果您有一个删除此切换案例的想法,可以通过将第一个数组替换为[this.getCorps(), this.getDisciplines(), this.getDepartements()]并同时执行这三个函数而无需切换情况太棒了!

Awsome solution for your problem would be using effects from Reactive extension for Angular called ngrx. 对于您的问题最棒的解决方案是使用Angular的Reactive extension的 effects称为ngrx。

Despite providing store managment it is also concentrated on triggering all state changes. 尽管提供商店管理,但它也集中于触发所有状态更改。 Here's example from my app how could your code(considering some refactor) look like after implementing it using ngrx: 这是我的应用程序中的示例,使用ngrx实现代码后,您的代码(考虑了一些重构)看起来如何:

@Effect()
someObservable$: Observable<Action> = this.actions$.ofType(IDENTIFIER_FOR_CALLING_ALL_3_FUNCTIONS).pipe(
    .forkJoin(() => [service.getCorps(), service.getDisciplines(), service.getDepartements()])
        .catch(err => {
            return Observable.of(new FAILURE_ACTION_IDENTIFIER());
        })
    );

I'm not able to check if the above code is 100% accurately written but it hold the concept. 我无法检查上面的代码是否被100%准确地编写了,但是它包含了这个概念。 What is noticable here: 这里值得注意的是:

  • IDENTIFIER_FOR_CALLING_ALL_3_FUNCTIONS can be ANY action and there can be many of them, just place comma between them. IDENTIFIER_FOR_CALLING_ALL_3_FUNCTIONS可以是任何操作,并且可以有很多,只需在它们之间放置逗号即可。
  • any effect can trigger as much events as You want - here triggering all your methods - it's obvious they would need some refactor(for example - moving your methods to some Service ) 任何效果都可以触发任意数量的事件-在这里触发所有方法-很明显,它们将需要一些重构(例如-将方法移至某些Service

I know that it may not be well presented so I'm putting here introduction course to ngrx/store and ngrx/effects and also link to official Github repository 我知道它可能无法很好地展示出来,因此我在这里介绍了ngrx / store和ngrx / effects的入门课程,并且还链接到了官方的Github存储库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM