【发布时间】:2021-11-12 01:53:08
【问题描述】:
我目前正在发出一个返回某些文件和用户数据的请求。
对于每个文件,我想返回一个包含数据的对象,但其中一个键依赖于另一个请求。
我怎样才能发出这个请求,以便getData 函数等到内部可观察对象解析为一个值?
function getData() {
return forkJoin([
filesApiRequest),
userApiRquest
])
.pipe(map(([files, userInfo]) => {
return files.getFilesList()
.map((file) => {
const name = file.getName();
const importantInfo = importantInfoCall(userInfo.name, name); // returns an observable.
// How can I get the value from this before returning? I need this piece of data for each
// file in the files list.
return {
data1,
data2,
name,
...,
hasImportantInfo: importantInfo
};
})
}));
}
【问题讨论】:
-
我将
switchMap(而不是map)与forkJoin(switchMap的返回结果)结合使用。这里的想法是等待所有importantInfoCall调用(我假设它们本质上是异步的)完成,然后对每次调用的结果执行您需要的操作。您可能希望将每个调用结果与它所属的相关文件配对以作为上下文。
标签: javascript angular rxjs angular2-observables