I have an array of IDs. For eg: [1,2,3,4].
I would like to make parallel calls and get the result with forkJoin for each element of the array. But below code is not working for me.
forkJoin(
Array.from(this.question.attachments).map(attachment => {
return mergeMap((attachment) => ) // i am stuck here.
})
)
.pipe(
takeUntil(this.destroy$),
finalize(() => this.spinnerService.hide())
)
.subscribe((attachmentIds) => {
this.uploadedAttachments = attachmentIds;
this.onFilesAttached.emit(this.uploadedAttachments);
});
Can anyone help me how to achieve this? Thanks
You're almost there.
forkJoinfunction expects an array or object of observables. So you only need to return the observable from theArray#mapfunction. HTTP calls using AngularHttpClientreturns an observable by default. So there is no need for themergeMapoperator.Also the usage of
mergeMapis wrong here. It returns anOperatorFunctionand not an observable.Try the following
Also in case if you weren't aware, an arrow function with a single statement without curly braces returns the statement by default.
So the following
is same as writing