angular - forEach中的Angular Http请求,等待完成请求直到继续循环
问题描述
我正在遍历选定的文件,以便首先获取签名的 url,然后将其直接放入 S3 存储桶。我现在的问题是,我想等到 forEach 中的所有内容都完成,然后继续循环。
我现在遇到的是代码同时直接查询每个文件的签名 url。
谢谢你的帮助
result['files'].forEach(file => {
this.fileService.getSignedURL(this.vehicleId, file.name, file.type, file.size)
.then(res => {
this.pushFile(file, app, description, languageKey, file.name, res.path, res.signed_url)
});
});
解决方案
我假设 observable fromgetSignedURL()
使用toPromise()
. 如果是这样,请将其删除并将其作为可观察对象返回。
然后,您可以使用 将数组中的元素映射到可观察Array#map
对象,使用 RxJSfrom
函数和concatMap
运算符来依次触发对元素的请求。
尝试以下
from(
result['files'].map((file) =>
this.fileService.getSignedURL(this.vehicleId, file.name, file.type, file.size).pipe(
concatMap((res) =>
this.pushFile(file, app, description, languageKey, file.name, res.path, res.signed_url)
)
)
)
).subscribe({
next: res => { },
error: error => { }
});
推荐阅读
- javascript - Leaflet and Ordance Survey Open Data:在网页上映射
- python - 使用多个依赖文件将 .py 转换为 .exe
- react-native - 反应本机导航-无法在额外属性扩展上获取属性“RNNKotlinVersion”,因为它不存在
- python - Python - 开始写入文本文件的第二行
- powershell - 我可以单独获取 AD 帐户属性,但不能在 foreach 循环中获取?
- android - 无法在 kotlin 中创建视图模型类的实例
- angular - Typescript 上的 Drawflow 库
- html - Bootstrap-vue b-table 响应属性无法识别 lg 或 xl 值
- java - Run SonarScanner analysis with Java 11, run target code with Java 8
- wordpress - WordPress taxonomy in CPT URL