简体   繁体   English

使用 switchmap 和 forkjoin 链接 observables 不能按预期工作

[英]chaining observables with switchmap and forkjoin not working as expected angular typescript rxjs

im currently creating trying to create a system where if i successfully updated a request i trigger 3 observables in a forkjoin then once those are finished console.log('complete/or finish') otherwise if it is not do nothin.我目前正在创建尝试创建一个系统,如果我成功更新了一个请求,我会在 forkjoin 中触发 3 个可观察对象,然后一旦这些对象完成 console.log('complete/or finish') 否则如果它不是什么都不做。 currently though the observables are completing before the forkjoin is complete and im completely at a loss of what's happening,目前虽然可观察对象在 forkjoin 完成之前就完成了,我完全不知道发生了什么,

 copyAttachments(): Observable<any> { if (this.model.attachments.length > 0) { return this.attachmentService.copyAttachments(this.model.attachments, this.model.id); } return empty(); } uploadAttachments(): Observable<any> { this.showAttachmentsUploadingModal = true; const formData = new FormData(); if (this.formAttachments.length > 0) { this.showAttachmentsUploadingModal = true; this.formAttachments.forEach(file => { formData.append('files', file, file.name); }); } // uploading empty formData will still trigger creation of default folder structure return this.attachmentService.uploadAttachments(formData, this.model.id); } uploadCustomerData(): Observable<any> { this.showAttachmentsUploadingModal = true; const formData = new FormData(); if (this.formCustomerData.length > 0) { this.showAttachmentsUploadingModal = true; this.formCustomerData.forEach(file => { formData.append('files', file, file.name); }); return this.attachmentService.uploadCustomerData(formData, this.model.id); } return empty(); }

 handleAttachments(): Observable<any> { return forkJoin ( this.copyAttachments(), this.uploadCustomerData(), this.uploadAttachments() ) } updateRequest() { this.myservice.updateRequest(this.model).pipe( switchMap((saveResult: boolean) => { this.showSubmittingModal = false; if (saveResult === true) { return this.handleAttachments(); } else { return empty(); } }), ).pipe(finalize(() => { console.log('finish') })).subscribe( response => { // todo: do anything with this response from uploadAttachments()? this.showAttachmentsUploadingModal = false; }, error => { location.reload(); }, () => { console.log('complete') } ); }

the expected result is my service completes, then my forkjoin observables complete then it will console.log('finish')预期的结果是我的服务完成,然后我的 forkjoin observables 完成,然后它将 console.log('finish')

currently the result is the service completes.目前的结果是服务完成。 it console.logs('finish') then my forkjoin triggers它 console.logs('finish') 然后我的 forkjoin 触发器

The solution I found to this problem is instead of returning empty() or of() you need to return an empty object as an observable this way Forkjoin still maintains value so return of({}) and everything is fine我发现这个问题的解决方案不是返回 empty() 或 of() 你需要返回一个空的 object 作为可观察的,这样 Forkjoin 仍然保持值所以 return of({}) 一切都很好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM