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