Skip to content

Instantly share code, notes, and snippets.

@VanTigranyan
Created May 29, 2019 11:29
Show Gist options
  • Select an option

  • Save VanTigranyan/242c49c090e05bc6a6e3f35baa4ce257 to your computer and use it in GitHub Desktop.

Select an option

Save VanTigranyan/242c49c090e05bc6a6e3f35baa4ce257 to your computer and use it in GitHub Desktop.
nested observables in loops
public getKernelAttachments(kernelId: IDBId) {
this.waitingForFiles = kernelId;
this.service.getKernelAttachments(kernelId)
.pipe(
mergeMap((fileIds: IDBId[]) => {
const packsArray = fileIds.map((id) => {
return this.service.getKernelFilePack(id);
});
return forkJoin(...packsArray).pipe(
map((packs) => {
return packs;
}),
catchError((e) => {
return of(e);
}),
);
}))
.subscribe((packs) => {
packs.map((pack) => {
this.http.get(pack.url, {responseType: 'blob'})
.subscribe(
(f) => {
saveAs(f, pack.name);
this.notify.create(
'The file has saved successfully!',
'Your file downloaded successfully and is ready for using!',
'success',
);
this.waitingForFiles = null;
},
(e: HttpErrorResponse) => {
this.notify.create(
'Something went wrong!',
`Downloading has failed!<br> Status Code: ${e.status} <br> Message: ${e.statusText}`,
'error',
);
this.waitingForFiles = null;
},
);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment