[英]Proceeding conditionally to the next mergeMap? [ rxjs.Observable]
I'd like to convert the my nested Observables with the use of pipe,map,mergeMap, however there are conditions after which I want it to proceed我想使用管道、地图、合并地图来转换我嵌套的 Observables ,但是在某些条件之后我希望它继续进行
observableFunction is an Observable that loops through an array, once its finished you can move on to use the manipulated data observableFunction是一个循环遍历数组的 Observable,一旦它完成,您就可以继续使用操作过的数据
observableFunction(par1,par2)
.subscribe((resp) => {
i++;
//do stuff, manipulate resp
const lastIteration = someArray.length == i;
if (lastIteration) {
//do stuff
observableFunction(par1a, par2a)
.subscribe((resp) => {
observableHandleFunction(resp)
.subscribe((handledData) => {
h++;
const lastIterationSub = someNestedArray.length == h;
//do stuff, manipulate handledData
if (lastIterationSub) {
console.log("done");
});
});
}
});
How can I convert so that it does seem more easy to read by using the features of rxjs that are meant to be used in this scenario?如何通过使用本场景中要使用的 rxjs 功能进行转换,使其看起来更容易阅读? Like applying conditions to mergeMap?
喜欢将条件应用于mergeMap?
observableFunction(par1,par2)
.pipe(
mergeMap((res1) => {
//do stuff
CONDITION={
return observableFunction(par1a,par2a)
}
}),
mergeMap((res2) => return observableHandleFunction(res2))
)
.subscribe((res3) => {
h++;
//do stuff
const lastIterationSub = someNestedArray.length == h;
if (lastIterationSub) {
console.log("done");
}
});
With a solution something like this its not gonna work with the condition, leading to an error使用类似这样的解决方案,它不会在条件下工作,导致错误
First of all, nesting subscribe
blocks is always a bad idea.首先,嵌套
subscribe
块总是一个坏主意。
If your observableFunction
emits single values (rather than a whole array at once) and when its done it emits a complete value, you can use toArray
operator.如果您的
observableFunction
发出单个值(而不是一次发出整个数组),并且完成后它发出一个完整的值,则可以使用toArray
运算符。 So:所以:
observableFunction(par1,par2)
.pipe(toArray())
// Here you will have an array of items, now you can proceed with something like:
observableFunction(par1,par2)
.pipe(
toArray(),
mergeMap(array => {
// If neccesary you can do the same here etc
}))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.