Learning the reactive pattern for bulk operations
As usual, we have to consider our tasks as streams. As the task that we are going to perform is uploading the recipe image in the backend, let’s imagine a stream called uploadRecipeImage$
that will take the file and the recipe identifier as input and perform an HTTP request. If we have N files to be uploaded, then we will create N streams.
We want to subscribe to all those streams together, but we are not interested in the values emitted from each stream through the process. Instead, we only care about the final result (the last emission) – whether the file is uploaded successfully, or something wrong happens and the upload fails.
Is there an RxJS operator that gathers a list of Observables together to get a cumulative result? Thankfully, yes: we have the forkJoin
operator.
The forkJoin operator
The forkJoin
operator falls under the category of combination operators. If we look at the official documentation...