解决可观察到无法正常工作的多个承诺
我正在使用firebase存储,并且正在尝试通过函数调用加载所有资产。获取资产URL的唯一方法是呼叫getDownloadurl
返回承诺。我需要为每项资产打电话给它,但由于某种原因,我无法等待所有承诺要等待。
我认为从Mergemap
返回承诺会让它等待所有人,但事实并非如此。
我看了很多有关承诺和RXJ的问题,但我似乎无法弄清楚该代码出了什么问题。
getAssets() {
return this.authService.user$.pipe(
first(),
switchMap(user => defer(() => from(this.afs.storage.ref(`${user.uid}/assets`).listAll()))),
switchMap(assets => from(assets.items).pipe(
mergeMap(async (asset) => {
return new Promise((res, rej) => {
asset.getDownloadURL().then(url => {
const _asset = {
name: asset.name,
url,
};
this.assets.push(_asset);
res(_asset);
})
.catch((e) => rej(e));
});
}),
)),
map(() => this.assets),
);
}
...
this.getAssets().subscribe(assets => console.log(assets)); // this runs before all asset's url has been resolved
I'm using Firebase Storage and I'm trying to load all assets via a function call. The only way to get an assets url is to call getDownloadURL
which returns a promise. I need to call this for every asset but I can't make it wait for all promises to be done before continuing for some reason.
I thought returning a promise from mergeMap
would make it wait for all of them but that doesn't seem to be the case.
I've look at a number of questions regarding promises and RXJS but I can't seem to figure out what's wrong with the code.
getAssets() {
return this.authService.user$.pipe(
first(),
switchMap(user => defer(() => from(this.afs.storage.ref(`${user.uid}/assets`).listAll()))),
switchMap(assets => from(assets.items).pipe(
mergeMap(async (asset) => {
return new Promise((res, rej) => {
asset.getDownloadURL().then(url => {
const _asset = {
name: asset.name,
url,
};
this.assets.push(_asset);
res(_asset);
})
.catch((e) => rej(e));
});
}),
)),
map(() => this.assets),
);
}
...
this.getAssets().subscribe(assets => console.log(assets)); // this runs before all asset's url has been resolved
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
概述
mergemap
所有内部可观察物。它旋转N内部可观察的管道,并并行运行,并将所有值串出相同的耦合在管道底部(在这种情况下您的订阅语句)作为单个排放。因此,为什么
this.getAssets()。订阅(Assets => console.log(Assets))
在所有并行内部Mergemap
Pipes完成其单个计算之前运行,因为MERGEMAP不会在发出之前等待所有这些(在它们完成时它会一一发射)。如果要等待可观察到的管道完成,则需要使用forkjoin
。fork join
forkjoin
您有一组可观察的物品,只关心每个的最终发射值。一个常见的用例是,如果您希望在页面加载(或其他某些事件)上发出多个请求,并且仅在收到所有人的响应时才想采取行动。通过这种方式,它类似于您可能使用Promise.All的方式。解决方案
祝您好运,并享受您的瑞典肉丸!
Overview
mergeMap
doesn't wait for all internal observables. It spins up n internal observable pipes that run in parallel, and spits all the values out the same coupling at the bottom of the pipe (your subscribe statement in this case) as individual emissions. Hence whythis.getAssets().subscribe(assets => console.log(assets))
runs before all your parallel internalmergeMap
pipes complete their individual computations, because mergeMap doesn't wait for all of them before emitting (it will emit one by one as they finish). If you want to wait for n observable pipes to finish, then you need to useforkJoin
.Fork Join
forkJoin
is best used when you have a group of observables and only care about the final emitted value of each. One common use case for this is if you wish to issue multiple requests on page load (or some other event) and only want to take action when a response has been received for all. In this way it is similar to how you might use Promise.all.Solution
Good luck out there, and enjoy your Swedish meatballs!
使用
concatmap
一个一一运行。use
concatMap
to run one by one.