javascript - 避免嵌套 Observables(完成时的 mergeMap)
问题描述
得到了一个抓取服务的伪代码,它首先探索分页 url,然后是抓取的“父”网站上的子页面 url
const pagination = [["url-1", "url-2", "url-3", "url-4"], ["url-5"]];
const timeInterval = 2;
sendRequestsInIntervals(pagination, timeInterval )
.pipe()
.subscribe({
next(x) {
console.log(x);
},
error(err) {
console.error(err);
},
complete() {
console.log("done");
sendRequestsInIntervals(resDataArray, timeInterval ).subscribe({
next(x) {
console.log(x);
},
});
},
});
我想避免嵌套,因为它是使用 observables 的不正确方式
有没有办法把它转换成这样的东西:
sendRequestsInIntervals(pagination, timeInterval )
.pipe(
waitUntilCompletes()
mergeMap((resDataArray) => {
return sendRequestsInIntervals(resDataArray, timeInterval );
})
)
.subscribe({
next(x) {
console.log(x);
},
error(err) {
console.error(err);
},
complete() {
console.log("done");
},
});
添加了一个名为waitUntilCompletes()的伪函数
rxJS 中是否有这样的事情,它使 mergeMap 中的 observable 在运行之前等待,直到前一个 observable 完成?
解决方案
操作员last
将忽略所有排放,直到完成后的最后一个排放:
sendRequestsInIntervals(pagination, timeInterval )
.pipe(
last(),
mergeMap((resDataArray) => { // resDataArray is only the final emitted value from sendRequestsInIntervals()
return sendRequestsInIntervals(resDataArray, timeInterval );
})
)
.subscribe({
next(x) {
console.log(x);
},
error(err) {
console.error(err);
},
complete() {
console.log("done");
},
});
或者您可以使用reduce
收集排放物并在完成后排放:
sendRequestsInIntervals(pagination, timeInterval )
.pipe(
// this will gather all outter emissions into an array and emit once outter completes
reduce((acc, val) => acc.concat(val), []),
mergeMap((resDataArray) => { // resDataArray is all values emitted from sendRequestsInIntervals() in an array
return sendRequestsInIntervals(resDataArray, timeInterval );
})
)
.subscribe({
next(x) {
console.log(x);
},
error(err) {
console.error(err);
},
complete() {
console.log("done");
},
});
推荐阅读
- php - 如何在选项卡中分配或获取循环以创建图库
- bash - 如何忽略 bash 标准输入重定向的错误?
- r - 如何从模块内部访问用户输入?
- reactjs - 将 React 作为内容脚本和 Chrome 扩展中的弹出窗口注入
- angular - 如何根据浏览器时区将偏移量应用于 UTC 时间。?
- c++ - 从 C++ 的数组代码中调试我的三元组总和中的错误
- javascript - Jquery没有加载本地html文件
- c# - 无法通过 Linq 方法 Contains() 针对 char 数组检查字符串,因为该数组不是 String 类型,转换为 String 类型失败
- amazon-web-services - 如何计算某个日期范围内我在 cloudwatch 中的日志中出现的次数?
- powershell - 带有powershell的itextsharp图层/重叠/邮票页面