【发布时间】:2019-02-05 14:41:40
【问题描述】:
我需要订阅一个结果,但要等待中间操作完成才能获得结果。诀窍是我“访问”我的结果来填充它:
// a service that gets a model
service.getModel(): Observable<MyModel>;
// I need to enrich my model before consuming it
service.getModel()
.makeSureAllCodesAreFetched(data => visitModel(model))
.subscribe(data => console.log("data is ready: ", data));
// a visitor that visits the model tree and enriches the leaves
// recursively visit the branches
visitModel(model: MyModel) {
if (model.isLeaf) {
// on condition, call a service to fetch additional data
service.fetchCodes(model.codeKey).subscribe(codes => model.codes = codes);
} else {
model.properties.forEach(prop: MyModel => visit(prop));
}
}
我尝试使用合并和 forkJoin() 没有成功。我只想确保在订阅我的数据之前完成对fetchCodes() 的所有调用,无论结果如何。
【问题讨论】:
-
请编辑问题并添加您使用
forkJoin()尝试的内容 - 您可能已经接近可行的解决方案。