javascript - Angular/RxJS - 将一个 promise 和内部 observable 转换为一个要返回的 observable
问题描述
我有一个承诺,一旦它实现,就会创建并订阅一个 observable。我需要做的是将这个 Promise 转换为一个 observable 并返回这个 observable 以便在其他地方订阅(这将包括内部 observable)。我曾经from
将 promise 转换为 observable,然后用于concatMap
链接内部 observable。但是,在开始时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);
}
})
);
})
);
}
解决方案
我认为你只需要返回你在里面创建的 Observable concatMap
;-)
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);
}
})
);
})
);
}
否则,它看起来不错!
推荐阅读
- r - 如何安装许多 R 包?
- powerbi - PowerBI DAX – 同一张表的子查询
- python - 无法使用 virtualenv 或 pipenv 创建虚拟环境
- javascript - 如何使用异步制作自己的等待功能
- java - Spring Data MongoDB 看不到来自父类的私有字段的访问器
- c# - 样式 aspx 导航菜单与悬停时的子菜单
- kdb - KDB 查询返回更多 2 列而不是 1 用于最大过滤器
- javascript - 带有大量内容的 jQuery 工具提示
- javascript - 防止在 JavaScript 中冒泡和捕获事件触发
- python - Python打开模式在写入数据之前重置文件