angular - Angular - 检查列表中的所有订阅者是否都已完成
问题描述
我使用间隔在 forEach 循环中将可变数量的数据发布到后端。我想要做的是:如果 post 方法的第一次调用没有完成但间隔已经想第二次调用该方法,则应该阻塞调用,直到第一次调用完成。
我尝试在代码中显示这一点
setInterval(() => {
console.log('pendingServiceDataRequests', this.pendingServiceDataRequests);
if(/* if pendingServiceDataRequests is empty or all subscriber are finished */){
this.sendData();
}
}, 5000);
sendData(){
serviceList = [/* some data */]
serviceList.forEach((service, index, array) => {
const currentSub = this.api.post(url, service).subscribe((res: any) => {
/* delete successful send data */
}
this.pendingDataRequests.push(currentSub);
});
}
我将所有订阅者放在一个列表中,但我不知道如何检查所有请求是否已完成
解决方案
对于这种情况,ConactMap 将是正确的。尝试这个:
sendData() {
serviceList = [/* some data */]
return from(serviceList).pipe(
concatMap(id => <Observable<Item>> this.api.post(url, service)
);
}
推荐阅读
- python - python烧瓶和pyrebase中的Seek()错误
- python - 在 Flask 中运行 discord.py 异步函数
- redirect - 如何将所有子域重定向到主页
- redis - 将 redis.conf 文件移动到 AMI 时,Packer 给我 SCP 权限被拒绝
- numpy - ValueError:检查输入时出错:预期 conv2d_16_input 有 4 个维度
- python - 使用 TensorFlow 进行多元线性回归
- django - Django 两个依赖下拉列表
- django - 我如何使用 django rest 框架返回可序列化的 json?
- php - 使用 sum,group by 从不同的表访问查询
- html - 从 python 循环结果到 django