[英]Angular/RxJS - Converting a promise and inner observable to one single observable to be returned
我有一個 promise,一旦它完成,就會創建並訂閱一個 observable。 我需要做的是將此 promise 轉換為可觀察對象,並將此可觀察對象返回以在其他地方訂閱(包括內部可觀察對象)。 我使用from
將 promise 轉換為可觀察對象,然后使用concatMap
鏈接內部可觀察對象。 但是,在concatMap
的開頭,我的代碼中出現了我試圖從初始 promise 檢索的xfdfString
字符串變量的錯誤。 也許我的新函數的內部可觀察對象實際上並沒有返回可觀察對象本身? 我已經嘗試了一些方法來解決這個問題,但沒有運氣,所以任何想法都會非常感激。
錯誤:
Argument of type '(xfdfString: string) => void' is not assignable to parameter of type '(value: string, index: number) => ObservableInput<any>'.
Type 'void' is not assignable to type 'ObservableInput<any>'.ts(2345)
原裝 function:
save() {
const { annotManager } = this.wvInstance;
annotManager.exportAnnotations({ links: false, widgets: false }).then(xfdfString => {
const requestId = (<SignatureDocumentData>this.task.actionData).requestId;
const data: SignDocument = { encodedDocument: encodeBase64(xfdfString) };
this.documentService.signDocument(requestId, data)
.pipe(
switchMap(signResponse => this.workflowService.completeAction(this.task.id)),
switchMap(nextTask => {
if (!nextTask) {
return this.workflowService.completeFolder();
} else {
return observableOf(nextTask);
}
}),
).subscribe(response => console.log(response));
});
}
我嘗試使用更高階的 observable 來代替返回一個 observable:
save(): Observable<any> {
const { annotManager } = this.wvInstance;
const docViewerobservable = from(annotManager.exportAnnotations({ links: false, widgets: false }));
return docViewerobservable.pipe(
concatMap(xfdfString => {
const requestId = (<SignatureDocumentData>this.task.actionData).requestId;
let data = { encodedDocument: encodeBase64(xfdfString) };
this.documentService.signDocument(requestId, data)
.pipe(
switchMap(signResponse => this.workflowService.completeAction(this.task.id)),
switchMap(nextTask => {
if (!nextTask) {
return this.workflowService.completeFolder();
} else {
return observableOf(nextTask);
}
})
);
})
);
}
我認為您只需要返回您在concatMap
中創建的 Observable ;-)
save(): Observable<any> {
const { annotManager } = this.wvInstance;
const docViewerobservable = from(annotManager.exportAnnotations({ links: false, widgets: false }));
return docViewerobservable.pipe(
concatMap(xfdfString => {
const requestId = (<SignatureDocumentData>this.task.actionData).requestId;
let data = { encodedDocument: encodeBase64(xfdfString) };
return this.documentService.signDocument(requestId, data)
.pipe(
switchMap(signResponse => this.workflowService.completeAction(this.task.id)),
switchMap(nextTask => {
if (!nextTask) {
return this.workflowService.completeFolder();
} else {
return observableOf(nextTask);
}
})
);
})
);
}
否則,它看起來不錯!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.