I have http observable and I want to add a timer to run parallel with it. For example, I want to show loading in at least 3s, so if http response before that, I want it to wait till timer emit after 3s. But if http took too long, say more than 5s, I'd like to throw an timeout error.
My code looks like this:
const tracker$ = timer(3e3).pipe(take(1));
const timeout$ = timer(5e3).pipe(take(1), map(() => { throw new Error('timeout!!!') }));
const query$ = combineLatest(http$, tracker$).pipe(map(([httpRes, tracker]) => (httpRes)));
this.loadData$ = race(timeout$, query$).pipe(// do next)
I'm using combineLatest
and it works. But if http$
response with error in less than 3s (before tracker$
emits value), then combineLatest
complete immediately with error, without waiting tracker$
to emit.
pipe catchError
to http$
seems to keep it going but I also want to throw error because I want to catch error after the race and do something about it.
How can I archive this 3s effect?