08 Jun 2019
5 min

RxJS recipes: ‘forkJoin’ with the progress of completion for bulk network requests in Angular

As it often happens to me — this post is just a prolongation of my StackOverflow activity:-).

Question:

Is there a way to gather a list of observables together to get cumulative result like forkJoin does, but get some kind of a progress while they finishes?

A nice task for creating custom RxJS function. Let's implement it!

What is forkJoin?

Let's take a look at official documentation:

__****forkJoin(…sources: any\[\]): Observable<any>****__

Accepts an Array of [ObservableInput](https://rxjs-dev.firebaseapp.com/api/index/type-alias/ObservableInput) or a dictionary Object of [ObservableInput](https://rxjs-dev.firebaseapp.com/api/index/type-alias/ObservableInput) and returns an [Observable](https://rxjs-dev.firebaseapp.com/api/index/class/Observable) that emits either an array of values in the exact same order as the passed array, or a dictionary of values in the same shape as the passed dictionary.

In other words, forkJoin for Observables works in the same way as Promise.all works for Promises.

Here is a marble diagram:

The possible use-case for it is running many parallel networks requests — for example, fetching some usersList details (if API supports only per user fetch).

Here is an example:

snippet link

Here is a codepen link to play with.
It works nice but what if we need to know some intermediate information like how many requests are resolved already? With current forkJoin implementation, it is impossible but we can create our own version of it ?.
OK, so brief agenda how this forkJoinWithPercent should work:
Output:
1. It returns higher-order(HO) Observable ( ) that emits an array of two Observables: [finalResult$, percent$].

*Higher-order (HO) Observable - observable that emits other observables, so data flow should be handled with special flattening operators like mergeMap, switchMap, concatMap, etc... you can read more about it here and here.

2. percent$ — emits percentage of completion (number);
3. finalResult$ — emits a final array of values at the end (or error if some argument observable emits error value).
Preliminarily usage looks like this:

snippet link

Implementation details:

  1. Our forkJointWithProgress function accepts an array of Observables and should return higher-order Observable.
  2. This returned Observable should emit value [finalResult$, percent$] (we will use of([finalResult$, percent$]) for that)
  3. Side effects will be added to each item in an array of argument Observables to calculate the percentage of completion and emit this value with special percent$ (we will use finalize operator for percentage calculation and use Subject as percent$).
  4. finalResult$ provides standard RxJS forkJoin result.
  5. Result Observable should work independently for each subscriber (wrap functionality with RxJS defer function to provide a clean run for each subscriber — you can read more about this case here).
  6. If some of the argument Observables emits error — it will be propagated to finalResult$ subscribers.

Packtpub.com and I prepared a whole RxJS course with many other details of how you can solve your every-day developer’s tasks with this amazing library. It can be interesting for beginners but also contains advanced topics. Take a look!


Implementation

#1–2 We accept an array of Observables and should return higher-order Observable with [finalResult$, percent$].

Ok, just a footprint of our future function:

snippet link%250A%257D)

Given we got arrayOfObservables (our ajax observables), so let’s iterate over and add requested logic:

#3 Adding side effects to argument Observables and calculating percentage.

Here is what we’ve got:

snippet link

We will add more functionality to argument observables in array. We iterate over an array and apply finalize operator for each observable.

finalize operator waits till specific observable completes, then calculate the percentage of completed observables and emit value with percent$Subject.

#4 Let's call forkJoin for getting a final result value

snipper link

Ok, let's go through this code:
forkJoin gets an array of argument observables, subscribes to all of them and waits till they are complete.
Once a result is ready — tap operator emits last percentage value (100%) and completes percentage$ Subject (to prevent memory leaks).
Final results will be emitted to subscribers.

5 Wrapping all this function in ‘defer’

RxJS defer function provides new Observable instance (a result of its callback function) for each subscriber. This means that each of our subscribers will get clean run (and counter = 0).

snippet link

Lets review how it works:

  1. We get an array of observables (line1).
  2. Wrap result with RxJS defer to provide a clean run for each subscriber (line 3)
  3. Create counter to calculate the percentage of completion and instantiate percent$ Subject to emit percentage value (lines 5–6).
  4. We create a new array by iterating over the original array and adding new functionality with finalize – calculating percentage value and emitting it if some of the observables completes. (lines 8–15)
  5. Call Rx forkJoin and apply tap operator to be able to send percentage 100 when forkJoin gets a final result. Assign result to finalResult$variable (lines 17–22).
  6. Return higher-order observable that will emit [finalResult$, percent$].

To wrap up

Here is how it works in a codepen:

forkJoinWithPercent

What about using it in Angular?

This function is published as an npm package as well — rxjs-toolbox so you can use it in your projects.

And here is a Stackblitz Angular demo that uses that package to feed percentage value to load bar:

In Angular

Additional reading.

You can read more about RxJS operators uses cases here:

  1. “Retry vs Repeat”
  2. “RxJS: Managing Operator State” by Nicholas Jamieson.
  3. “RxJS ‘repeat’ operator — beginner necromancer guide”
  4. “Throttling notifications from multiple users with RxJS”
  5. rxjs-toolkit — RxJS Everyday Custom Operators by Jason Awbrey.
  6. backoff-rxjs — A collection of helpful RxJS operators to deal with backoff strategies by Alex Okrushko.

Like this article? Let’s keep in touch on Twitter!


Starting from section 4 of my RxJS video course advances staff is reviewed — so if you familiar with RxJS already — you can find something useful for you as well: higher-order observables, anti-patterns, schedulers, unit testing, etc! Give it a try!

Special thanks to Lars Gyrup Brink Nielsen, Nicholas Jamieson, Tim Deschryver and Michael Karén for reviewing this post and making many valuable remarks to make it better!

Frequently asked questions

How can I track progress while Angular forkJoin requests are running?

Use a custom forkJoinWithProgress function that returns both the final forkJoin result and a progress Observable. It adds finalize to each request Observable to update a percentage as requests finish, then emits 100% when forkJoin produces its final result. In Angular, the progress stream can be used to update a loading bar.

How does forkJoinWithProgress calculate the completion percentage?

forkJoinWithProgress applies finalize to each input Observable and uses a counter to calculate the percentage as those Observables finish. It emits the percentage through a Subject exposed as percent$. When forkJoin produces its final result, a tap emits 100% and completes the progress Subject.

Why should forkJoinWithProgress use RxJS defer()?

Wrap the implementation in defer() so each subscriber gets a fresh run of forkJoinWithProgress. The callback creates a new counter and progress Subject, preventing subscribers from sharing progress state from an earlier subscription. Without that fresh setup, the counter would not start cleanly for each subscriber.

What happens if one request errors in forkJoinWithProgress?

If an input Observable errors, the error is propagated to subscribers of finalResult$, rather than producing the final array of results. The function’s final results come from standard forkJoin; the separate percent$ stream reports progress, while the article specifies error propagation for finalResult$.

How do I use forkJoinWithProgress in an Angular project?

The article says forkJoinWithProgress is available through the rxjs-toolbox npm package and demonstrates using it in an Angular example to feed a percentage value to a loading bar. The function returns a higher-order Observable that emits [finalResult$, percent$], so handle that outer Observable with an appropriate flattening operator.

Share this post

Sign up for our newsletter

Stay up-to-date with the trends and be a part of a thriving community.