Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 异步等待vs承诺vs映射?_Javascript_Angular_Typescript - Fatal编程技术网

Javascript 异步等待vs承诺vs映射?

Javascript 异步等待vs承诺vs映射?,javascript,angular,typescript,Javascript,Angular,Typescript,如何在承诺、异步等待和像concatMap这样的映射操作符之间做出决定 这是我的具体情况,但我也很好奇你一般如何决定: 我正在对后端进行http调用,然后再进行另一个http调用。在处理第二次调用的json数据时,我需要使用第一次调用返回的值。在这种情况下,使用AsyncWait、promise还是concatMap更好?一般来说,决定使用哪种方法的指导原则是什么 以下是我目前使用的concatMap。(我正在从getTask http调用动态生成子组件,每个子组件都需要访问annotation

如何在承诺、异步等待和像concatMap这样的映射操作符之间做出决定

这是我的具体情况,但我也很好奇你一般如何决定:

我正在对后端进行http调用,然后再进行另一个http调用。在处理第二次调用的json数据时,我需要使用第一次调用返回的值。在这种情况下,使用AsyncWait、promise还是concatMap更好?一般来说,决定使用哪种方法的指导原则是什么

以下是我目前使用的concatMap。(我正在从getTask http调用动态生成子组件,每个子组件都需要访问annotationFormats)


它们有不同的用途。async/await在您希望保留已编写异步代码的位置时使用。而primies是一种工具,用于发现执行异步代码并调用回调的位置。

async/await和promise基本相同,但语法不同。在某些作业完成后将运行一次的异步代码

作为一项规则,我永远不会在使用Angular时不使用这些。Angular附带了RxJS开箱即用,这比承诺多得多。作业完成后,您可以使用RxJS运行异步代码一次,但它还提供了创建数据流并以多种不同方式对其进行操作的可能性。 完全理解RxJS和反应式编程确实需要一点时间,但一旦你了解了它,你就会意识到你能做多少

在您的情况下,我喜欢使用操作符
forkJoin
,因为这两个请求似乎彼此独立。您可以为它提供一个您想要获取的资源列表,并在完成后在subscribe中执行异步代码,这使它非常适合http请求:

forkJoin({
  annotationFormats: this.dashboardService.getAnnotationFormats(),
  tasks: this.dashboardService.getTasks(),
})
.subscribe(
  ({tasks, annotationFormats})=>{
      for(let task of tasks){
        const componentFactory=this.CFR.resolveComponentFactory(DashboardItemComponent);
        const componentRef=this.vc.createComponent(componentFactory);
        componentRef.instance.task=task;
        componentRef.instance.annotationFormats=annotationFormats;
        componentRef.instance.compInteraction=this;
        this.taskRef.push(componentRef);
      }
    }
);
花点时间学习RxJS,我保证会有回报的。每当您使用RxJS时,它感觉太复杂或错误,这是因为它可能是。前往RxJS文档,寻找一些可能有用的东西,如果你找不到任何东西,谷歌快速搜索可能会为你找到解决方案。关键是,不要只是盲目地使用它,要始终试着理解它是如何工作的

我希望这是有用的

编辑:

对于RxJS<6.5,语法有点不同:

forkJoin(
  this.dashboardService.getTasks(),
  this.dashboardService.getAnnotationFormats()
)
.subscribe(
  ([tasks, annotationFormats])=>{
      for(let task of tasks){
        const componentFactory=this.CFR.resolveComponentFactory(DashboardItemComponent);
        const componentRef=this.vc.createComponent(componentFactory);
        componentRef.instance.task=task;
        componentRef.instance.annotationFormats=annotationFormats;
        componentRef.instance.compInteraction=this;
        this.taskRef.push(componentRef);
      }
    }
);

请注意,我们将资源作为参数而不是对象传递,订阅中的结果也将以数组形式而不是对象形式传递。

非常感谢!这太棒了!啊,当我尝试这段代码时,我遇到了一个错误:{annotationFormats:Observable;tasks:Observable;}类型的参数不能分配给“ObservableInput”类型的参数。Object literal只能指定已知的属性,而“ObservableInput”类型中不存在“annotationFormats”。嘿,Shisui,很抱歉回复太晚。我已经在一个真实的场景中测试了我的解决方案,它似乎工作得很好,给你:(检查控制台)。也许你可以检查一下代码和你的代码有什么不同,这样我们就可以发现问题了?我以前从未遇到过这个错误。事实上,查看文档,问题可能是您的rxjs版本。大于6.5吗?如果不是,你将不得不使用不同的语法,我可以用其他语法更新原始帖子。
forkJoin(
  this.dashboardService.getTasks(),
  this.dashboardService.getAnnotationFormats()
)
.subscribe(
  ([tasks, annotationFormats])=>{
      for(let task of tasks){
        const componentFactory=this.CFR.resolveComponentFactory(DashboardItemComponent);
        const componentRef=this.vc.createComponent(componentFactory);
        componentRef.instance.task=task;
        componentRef.instance.annotationFormats=annotationFormats;
        componentRef.instance.compInteraction=this;
        this.taskRef.push(componentRef);
      }
    }
);