如果我有这样的数组数组
{
parent: [
{
name: 'stu',
children: [
{name: 'bob'},
{name: 'sarah'}
]
},
{
...
}
]
}
Run Code Online (Sandbox Code Playgroud)
并且我想循环遍历每个父级并循环遍历他们的子系列,这样我就不会启动下一个父级,直到所有子级都被处理(一些长的异步过程),我该如何使用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);
Run Code Online (Sandbox Code Playgroud)
但我让所有的父母都退出了,然后是所有的孩子.
concatMap这里效果更好.因为如果迭代子项是异步的,则子项的顺序将被搞砸.concatMap可以确保一次完成一个父母.
Rx.Observable.from(parents)
.concatMap(function (p) {
return Rx.Observable.from(p.children)
})
.subscribe();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1599 次 |
| 最近记录: |