【发布时间】:2020-08-04 14:53:08
【问题描述】:
我有一个承诺,一旦它实现,就会创建并订阅一个 observable。我需要做的是将这个 Promise 转换为一个 observable 并返回这个 observable 以便在其他地方订阅(这将包括内部 observable)。我使用from 将promise 转换为可观察对象,然后使用concatMap 链接内部可观察对象。但是,在concatMap 的开头,我在尝试从初始承诺中检索的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)
原函数:
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);
}
})
);
})
);
}
【问题讨论】:
标签: javascript angular typescript rxjs