RxJS 排队依赖任务

编程入门 行业动态 更新时间:2024-10-12 03:20:36
本文介绍了RxJS 排队依赖任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如果我有这样的数组

{ parent: [ { name: 'stu', children: [ {name: 'bob'}, {name: 'sarah'} ] }, { ... } ] }

并且我想循环遍历每个父级并依次循环遍历他们的子级,这样在所有子级都处理完毕之前我不会启动下一个父级(一些长的异步过程),我该如何使用 RxJS 执行此操作?

and I want to cycle through each parent and cycle through their children in series, so that I don't start the next parent until all the children have been processed (some long asynchronous process), how do I do this with RxJS?

我已经尝试过:

var doChildren = function (parent) { console.log('process parent', parent.name); rx.Observable.fromArray(parent.children) .subscribe(function (child) { console.log('process child', child.name); // do some Asynchronous stuff in here }); }; rx.Observable.fromArray(parents) .subscribe(doChildren);

但是我让所有的父母都退出,然后所有的孩子都退出了.

But I get all the parents logging out and then all the children.

推荐答案

concatMap 在这里效果更好.因为如果迭代子节点是异步的,子节点的顺序就会混乱.concatMap 可以保证一次完成一个父节点.

concatMap works better here. Because if iterating children is async, the order of child will be messed up. concatMap can ensure to finish one parent at a time.

Rx.Observable.from(parents) .concatMap(function (p) { return Rx.Observable.from(p.children) }) .subscribe();

更多推荐

RxJS 排队依赖任务

本文发布于:2023-10-18 23:25:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1505721.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:RxJS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!