【发布时间】:2021-12-30 07:05:01
【问题描述】:
我正在尝试为这样的场景编写 angular 11 的代码 -
我有文件列表,对于我点击 api(比如 api1)的每个文件,我从响应中获取一个 fileId,然后将它传递给另一个 api(比如 api2),我想继续每 3 次点击 api2秒,除非我在响应中没有得到 status="available"。一旦我获得了可用状态,我不再需要为该 fileId 访问 api2,我们可以开始处理循环中的下一个文件。
我拥有的每个文件的整个过程。
我知道我们可以使用 rxjs 操作符(如 mergeMap 或 switchMap)来实现这一点(因为序列现在对我来说并不重要)。但我对 rxjs 很陌生,不知道如何组合起来。
这就是我现在正在做的-
this.filesToUpload.forEach((fileItem) => {
if (!fileItem.uploaded) {
if (fileItem.file.size < this.maxSize) {
self.fileService.translateFile(fileItem.file).then( //hit api1
(response) => {
if (response && get(response, 'status') == 'processing') {
//do some processing here
this.getDocumentStatus(response.fileId);
}
},
(error) => {
//show error
}
);
}
}
});
getDocumentStatus(fileId:string){
this.docStatusSubscription = interval(3000) //hitting api2 for every 3 seconds
.pipe(takeWhile(() => !this.statusProcessing))
.subscribe(() => {
this.statusProcessing = false;
this.fileService.getDocumentStatus(fileId).then((response)=>{
if(response.results.status=="available"){
this.statusProcessing = true;
//action complete for this fileId
}
},(error)=>{
});
})
}
【问题讨论】:
标签: javascript angular rxjs rxjs-observables