How to pass results between chained observables

前端 未结 7 1893
一整个雨季
一整个雨季 2020-12-29 04:03

Abstract problem: Every time a source Observable emits and event, a sequence of API calls and Angular services need to be triggered. Some of those invocatio

相关标签:
7条回答
  • 2020-12-29 04:30

    As far as I understood you, you are concerned about readability and not having to carry the payload from method to method.

    Have you ever thought about converting an Observable to a Promise? The important thing here is that the observables must complete so that the promise is fulfilled and can be resolved (is the same as complete but only for promise).

    Due to your advice, see above (like with async await) I came to this suggestion.

    private async startUpload(event: StartUploadEvent) {
        const headers = await this.getAuthenticationHeaders(event).toPromise();
        const id = await this.generateUploadId().toPromise();
        
        this.emitUploadStartEvent(id, event);
        
        const pdfId = await this.createPdfDocument(event, headers, id).toPromise();
        await this.uploadBilderForPdf(event, pdfId, headers, id).toPromise();
        
        const cloudId = await this.closePdf(headers, pdfId).toPromise();
        this.emitUploadDoneEvent(id, event, cloudId)
        
        return cloudId
    }
    

    Info: Here you can read what happens if you convert an observable into a promise without having complete the observable: Why converted promise from Subject (Observable) does not work as expected

    Note: I'm fulfilling your expectations according

    And maybe there are other ways to solve the problem which are not violating common best practices

    0 讨论(0)
提交回复
热议问题